Jump to content

[SOLVED] What's wrong with this preg_replace?


AdRock

Recommended Posts

I am trying to replace part of a string with another part but i get warnings and it doesn't work.

 

Basically i want to strip any <br> or <br /> tags after a closing <img> tag.

 

When i output the un replaced string and the replaced string, they are both the same so something has gone wrong but i can't see what.

 

Output from browser (source)

<br />

<b>Warning</b>:  preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Unknown modifier '>' in <b>d:\Apache\htdocs\test.php</b> on line <b>14</b><br />

<br />

<b>Warning</b>:  preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Unknown modifier '>' in <b>d:\Apache\htdocs\test.php</b> on line <b>14</b><br />

 

<br />

<b>Warning</b>:  preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Unknown modifier '>' in <b>d:\Apache\htdocs\test.php</b> on line <b>14</b><br />

<html>

<head>

</head>

<body>

<ul>

<li><img src="http://www.mysite.co.uk/images/imgage.jpg" class="imgright" alt="" /><br />fdgfdgdfgfdssagd<br /><br />gfgfdgsdgs<br /><br />dsfgsdgdsgs</li>

 

<li><img src="http://www.mysite.co.uk/images/imgage.jpg" class="imgright" alt="" /><br />fdgfdgdfgfdssagd<br /><br />gfgfdgsdgs<br /><br />dsfgsdgdsgs</li>

 

</ul>

 

and the code

<?php
function update_text($content) {
$reg_ex = array(
	'/.jpg" class="imgright" alt="" /><br />/s', 
	'/.gif" class="imgright" alt="" /><br />/s',
	'/<br /><br />/s'
);
$replace_word = array(
	'.jpg" class="imgright" alt="" />', 
	'.gif" class="imgright" alt="" />',
	'<br />'
); 

$content = preg_replace($reg_ex, $replace_word, $content);

return $content;
}

$data = '<img src="http://www.mysite.co.uk/images/imgage.jpg" class="imgright" alt="" /><br />fdgfdgdfgfdssagd<br /><br />gfgfdgsdgs<br /><br />dsfgsdgdsgs';
$data1 = html_entity_decode($data);

$content = update_text($data1);
?>
<html>
<head>
</head>
<body>
<ul>
<li><?php echo $data; ?></li>
<li><?php echo htmlentities($content); ?></li>
</ul>
</body>
</html>

 

This is just a test script to see where my app is going wrong

 

well...first thing is that the errors you post don't match the code you post....look into that

 

also, you need to pick encoded HTML or not....your regex uses regular html "<br />" etc, while your content you are trying to replace has the HTML characters encoded as ";<br />". preg sees them as strings, so you need to use one way or the other.

 

lastly, in the code you post, you need to escape regex characters like / with a \...the fixed regex would look like so:

<?php
function update_text($content) {
   $reg_ex = array(
      '/.jpg" class="imgright" alt="" \/><br \/>/s', 
      '/.gif" class="imgright" alt="" \/><br \/>/s',
      '/<br \/><br \/>/s'
   );
   $replace_word = array(
      '.jpg" class="imgright" alt="" />', 
      '.gif" class="imgright" alt="" />',
      '<br />'
   ); 
   
   $content = preg_replace($reg_ex, $replace_word, $content);
      
   return $content;
}

$data = '<img src="http://www.mysite.co.uk/images/imgage.jpg" class="imgright" alt="" /><br />fdgfdgdfgfdssagd<br /><br />gfgfdgsdgs<br /><br />dsfgsdgdsgs';
$data1 = html_entity_decode($data);

$content = update_text($data1);
?>

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.