A difficult preg_replace

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ngungo
Forum Commoner
Posts: 75
Joined: Thu Jun 08, 2006 10:45 pm

A difficult preg_replace

Post 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]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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().
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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);
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What are you trying to do here? You may find strtotime() and date() are easier than a regex.
Post Reply