preg_replace help ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

preg_replace help ??

Post by PHPycho »

Hello forums!!
Case:

Code: Select all

$db_row = array('name_field' => 'Tom', 'roll_field' => 22) ; //items from db
 
$string = 'my/name/[name_field]/roll/[roll_field]';
 
$final_string = preg_replace('/(\[(.*?)\])/',$db_row['$2'], $string); //I tried this, but gives notice error: Undefined index:  $2
 
echo $final_string; // expected output: my/name/Tom/roll/22
How to accomplish this using preg_replace.
Thanks.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: preg_replace help ??

Post by papa »

What happens if you use $1 ?
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: preg_replace help ??

Post by PHPycho »

same notice error occurs as:
Undefined index: $1 in
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: preg_replace help ??

Post by papa »

I suck at this but whatta hell:

preg_replace('/\[(.*?)\]/',$db_row['$1'], $string);

Removing the () ?

or

preg_replace('/\[(.*?)\]/',$db_row[$1], $string);
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: preg_replace help ??

Post by mintedjo »

Code: Select all

$db_row[$2]
?
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: preg_replace help ??

Post by PHPycho »

None works.
Any xtra options.
Thanks
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: preg_replace help ??

Post by Apollo »

You can't use $db_row['$2'] as a replacement string just like that. The $2 part would only be replaced after evaluating the array, and since $db_row['$2'] (literally) doesn't exist, you get an empty result.

You need the e regexp modifier, which will evaluate the replacement expression, and put your replacement string in quotes:

Code: Select all

$final_string = preg_replace('/(\[(.*?)\])/e','$db_row["$2"]', $string);
By the way, you can get rid of the outer set of ( ) since they're kinda useless here, and change $2 to $1:

Code: Select all

$final_string = preg_replace('/\[(.*?)\]/e','$db_row["$1"]', $string);
One small sidenote - you probably know this yourself (since you wrote the expression) but for others who may miss this: the ? in (.*?) is to make the * lazy instead of greedy. Without the ? it would catch more characters up to the last ]. So when regexping "[xxx]yyy[zzz]", instead of two parts "xxx" and "zzz", it will find one match: "xxx]yyy[zzz".
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: preg_replace help ??

Post by PHPycho »

Thanks Apollo.
That code is really awsome. and thanks for the hint too.
Post Reply