Page 1 of 1

PREG_REPLACE not working with array replacement

Posted: Thu Apr 27, 2006 10:29 pm
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!!!!

Posted: Thu Apr 27, 2006 11:25 pm
by feyd
$n is not valid PCRE referencing, nor is it in the replacement string.

Posted: Thu Apr 27, 2006 11:59 pm
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....

Posted: Fri Apr 28, 2006 12:20 am
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.

Posted: Fri Apr 28, 2006 1:15 am
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?

Posted: Fri Apr 28, 2006 1:21 am
by feyd
try it, and you will likely get a glimpse of the answer.

Posted: Fri Apr 28, 2006 5:55 pm
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.