Page 1 of 1

A difficult preg_replace

Posted: Sat Nov 24, 2007 10:37 am
by ngungo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


$daily_string contains date substrings such as 11-23-2007
monday() is a function which takes a date substring as its sole parameter and returns a string.

The following code does not work, just to demonstrate what I wish to do. Any helps appreciated.

Code: Select all

$daily_string = preg_replace('/(\d\d-\d\d-\d{4})/', monday($1), $daily_string);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Nov 24, 2007 10:44 am
by RobertGonzalez
I think, since you are using a function to manipulate the replacement, you are going to want to use a callback function. Try preg_replace_callback().

Posted: Sun Nov 25, 2007 7:26 am
by GeertDD
Using preg_replace_callback() is preferred, but you could use the e modifier as well.

Code: Select all

$daily_string = preg_replace('/\d\d\-\d\d\-\d{4}/e', 'monday(\'$0\')', $daily_string);

Posted: Mon Nov 26, 2007 8:59 pm
by Ollie Saunders
What are you trying to do here? You may find strtotime() and date() are easier than a regex.