tags:

views:

38

answers:

1

Hi guys,

So I am trying to create this function which replaces a certain symbol with something else. I was going to use ereg_replace but I saw that it has been deprecated in 5.3. Can you guys give me suggestion on what to use for this purpose.

To be more specific, I want to create a function that parses in an email which replaces a certain symbol in the email with the current date so like.

<date-1> 

will be yesterday's date and

 <date>

will be today's date.

Thanks!

+6  A: 

Use preg_replace() instead. This page highlights the differences: http://php.net/manual/en/reference.pcre.pattern.posix.php

And as for your specific problem, this should do:

$string = 'test <date> test2 <date-1> test3 <date+3>';

echo preg_replace_callback('#<date(?:([+-])(\d+))?>#', function($match) {
    if (!isset($match[1])) {
        return date('Y-m-d');
    }
    return date('Y-m-d', strtotime(sprintf('%s%d days', $match[1], $match[2])));
}, $string);
Daniel Egeberg