flea Posted August 13, 2009 Share Posted August 13, 2009 Hi, Sorry if this has been asked before, I did a search, but couldn't find a close match to my problem. Summary: Using preg_match with expression returns 2 matches, but using the same expression with preg_replace has no effect. Here is my code: <?php header ("Content-Type: text/javascript"); $html = '<head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>about this site</title> <style type="text/css"> <!-- body,td,th { font-family: Arial, Helvetica, sans-serif; } body { background-image: url(mw/w_back.jpg); } .style1 { font-size: 36px; font-weight: bold; } --> </style></head> <body> <a href="../">[bACK]</a><br> <table border="0" cellspacing="5" cellpadding="5"> <tr> <td align="center" valign="top"><table width="80%" border="0" align="center" cellpadding="5" cellspacing="5" bgcolor="#F0FCFF"> <tr> <td><p align="center" class="style1">wonderlandhistory.net</p> <p align="left">This website is dedicated to the blah blah blah </td> </tr> </table></td> </tr> </table> <a href="../">[bACK]</a> </body> </html> '; preg_match("/<body>(.*)</body>/s",$html, $body); //print_r($body)." "; $ibody = $body[1]; echo $ibody; echo " "; preg_match_all ('/<a href="../">[bACK]</a>/s', $ibody, $newbody); print_r($newbody); echo " "; preg_replace ('/<a href="../">[bACK]</a>/s',"",$ibody); echo $ibody; echo " "; ?> ------------------------------------------------------------------------------ Here is the (unexpected) output: <a href="../">[bACK]</a><br> <table border="0" cellspacing="5" cellpadding="5"> <tr> <td align="center" valign="top"><table width="80%" border="0" align="center" cellpadding="5" cellspacing="5" bgcolor="#F0FCFF"> <tr> <td><p align="center" class="style1">wonderlandhistory.net</p> <p align="left">This website is dedicated to the blah blah blah </td> </tr> </table></td> </tr> </table> <a href="../">[bACK]</a> Array ( => Array ( => <a href="../">[bACK]</a>[1] => <a href="../">[bACK]</a> ) ) <a href="../">[bACK]</a><br> <table border="0" cellspacing="5" cellpadding="5"> <tr> <td align="center" valign="top"><table width="80%" border="0" align="center" cellpadding="5" cellspacing="5" bgcolor="#F0FCFF"> <tr> <td><p align="center" class="style1">wonderlandhistory.net</p> <p align="left">This website is dedicated to the blah blah blah </td> </tr> </table></td> </tr> </table> <a href="../">[bACK]</a> ------------------------------------------------- Can someone tell me why preg_match finds both instances of <a href="../">[bACK]</a>, but preg_replace is unable to replace those instances using exactly the same expression? Will greatly appreciate any and all replies. Ian Quote Link to comment https://forums.phpfreaks.com/topic/170063-problem-with-preg_replace/ Share on other sites More sharing options...
thebadbad Posted August 13, 2009 Share Posted August 13, 2009 preg_replace() returns the modified string, and you aren't storing it $ibody = preg_replace ('/<a href="../">[bACK]</a>/s',"",$ibody); Quote Link to comment https://forums.phpfreaks.com/topic/170063-problem-with-preg_replace/#findComment-897208 Share on other sites More sharing options...
flea Posted August 14, 2009 Author Share Posted August 14, 2009 preg_replace() returns the modified string, and you aren't storing it $ibody = preg_replace ('/<a href="../">[bACK]</a>/s',"",$ibody); thanks, that works. Quote Link to comment https://forums.phpfreaks.com/topic/170063-problem-with-preg_replace/#findComment-897788 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.