phpregexpreg-replaceereg-replace

Conversion of ereg_replace to preg_replace, PHP 5,3


For changing to PHP 5,3 ... not sure if I can just change the function name in the below from ereg_replace to preg_replace - if that will result in the same or if the syntax has to be changed as well regarding "[\n\r]" og "\t\t+" - anyone who can tell me that for sure (very hard to test in the given setup environment)...

$line2 = ereg_replace("[\n\r]", "", $line);

$line2 = ereg_replace("\t\t+", "", $line2);

Thanks in advance...


Solution

  • the given examples should work like this:

    $line2 = preg_replace("/[\n\r]/", "", $line);
    
    $line2 = preg_replace("/\t\t+/", "", $line2);
    

    In some cases you need other modifications, too. For example if you used the / inside your regex then you need to escape it like this: \/

    please read this thread: ereg_replace to preg_replace?