Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 8, 2009, 15:07   #1
lsolesen
SitePoint Zealot
 
Join Date: Oct 2003
Location: Denmark
Posts: 109
ereg() deprecated so have to rewrite to preg_match()

A while back I used ereg() in some of my functions, but from php 5.3 ereg() is deprecated, so I need to rewrite them to preg_match(). However, I am not to good with regular expressions in preg_match()-style. Hoping for your help:

PHP Code:

ereg("^[a-z0-9]+$", $name);

// I think I rewrote it correctly to
preg_match("/^[a-z0-9]+$/", $name);
// and I guess the following should just be done in the same manner
ereg("^[A-Z][a-z]{2}$", $iso_code)
ereg('^[0-9]{2}-[0-9]{2}-[0-9]{4}$', $duration)
ereg('^([0-9]{1,2}) month$', $duration, $params)
A more complicated story - where I have no clue where to start when using preg_match():

PHP Code:

    // @todo this should be rewrote to preg_match

    // function converts a date to iso-db-format
    
function convert2db($default_year = "")
    {
        
$d = "([0-3]?[0-9])";
        
$m = "([0-1]?[0-9])";
        
$y = "([0-9][0-9][0-9][0-9]|[0-9]?[0-9])";
        
$s = "(-|\.|/| )";

        if (
$default_year == "") {
            
$default_year = date("Y");
        }

        if (
ereg("/^".$d.$s.$m.$s.$y."$/", $this->date, $parts)) {
            
// true
        
} elseif (ereg("/^".$d.$s.$m."$/", $this->date, $parts)) {
            
$parts[5] = $default_year;
            
// true
        
} else {
            return
false;
        }

        
$this->date = $parts[5]."-".$parts[3]."-".$parts[1];
        return
true;
    }
Thanks in advance for your help.
lsolesen is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 22:10.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved