Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PREG_REPLACE different from PREG_MATCH_ALL? 1

Status
Not open for further replies.

SCelia

Programmer
Feb 27, 2002
82
CA
Ok see if you can understand this:

$main_page_data is mixed html/php code like your average php webpage.

//notice these use the exact same pattern...
preg_match_all (&quot;/<\?(.+?)\?>/s&quot;,$main_page_data,$matches);

preg_replace (&quot;/<\?(.+?)\?>/s&quot;,'<BREAK>',$main_page_data);

$main_page_data = $main_page_data.'<BREAK>';

//this failed, php code was not replaced
echo $main_page_data;

//this worked, all the php code is printed out perfectly
foreach ($matches[1] as $match) {

highlight_string('<?'.$match.'?>');
echo &quot;\n<br><br><br>\n&quot;;

}


Why the discrepency?
Celia
 
You're using preg_replace wrong. It doesn't just change the input string. It makes a copy of the string and outputs it with changes if its patterns match.

Instead of:

preg_replace (&quot;/<\?(.+?)\?>/s&quot;,'<BREAK>',$main_page_data);

try:

$main_page_data = preg_replace (&quot;/<\?(.+?)\?>/s&quot;,'<BREAK>',$main_page_data);
 
haha

That's got to be one of the stupider things I've done!

Thanks. Celia
 
Don't feel bad. Care to guess how I spotted the problem so quickly?


If you guessed that it's because I fought the same problem once for 6 hours before I noticed the return type of the function, you'd be correct.
 
lol well atleast I didn't spend 6 hours on it:) I asked the experts :). Celia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top