PREG_REPLACE not working with array replacement

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

Moderator: General Moderators

Post Reply
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

PREG_REPLACE not working with array replacement

Post by Deseree »

Code: Select all

$quote[0] = "TEST 1";
$quote[1] = "TEST 2";
$quote[2] = "TEST 3";
$quote[3] = "TEST 4";
$quote[4] = "TEST 5";

$current_paragraph = "WHATEVER IS HERE THEN #QUOTE# BLAH BLAH BLAH BLAH #QUOTE# , I WANT MCDONALDS #QUOTE#";

$current_paragraph = preg_replace("/(#QUOTE#|#QUOTES#)/is",$quote[$n],$current_paragraph,-1);
Why doesn't the $n work in the replacement field?

http://us2.php.net/manual/en/function.preg-replace.php

says it should work but it's not working.... using php 4.3.2

Please help!!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$n is not valid PCRE referencing, nor is it in the replacement string.
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

feyd wrote:$n is not valid PCRE referencing, nor is it in the replacement string.
ok then how would I do what i'm trying to accomplish, because from what I read on that page I linked it looked like $n was a something valid you can use to determine which replacement number or kinda like count it's on....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There is no such functionality supported by regex. A carefully crafted callback function combined with preg_replace_callback() could do what you wish.
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

feyd wrote:There is no such functionality supported by regex. A carefully crafted callback function combined with preg_replace_callback() could do what you wish.
how about exploding based on #BREAK# then running through a for loop and adding the needed array part? is that more effecieint than callback?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try it, and you will likely get a glimpse of the answer.
Deseree
Forum Commoner
Posts: 84
Joined: Mon Feb 13, 2006 11:35 pm

Post by Deseree »

feyd wrote:try it, and you will likely get a glimpse of the answer.
yea I did try it and it works, I'm just looking for optimized as well, as It's like 3 total fo rloops.
Post Reply