Jump to content

Problem with preg_replace


flea

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/170063-problem-with-preg_replace/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.