Jump to content

Search and Replace (<br> for <br />)


nfr

Recommended Posts


I also need to perform the following on the text:

1.) replace all instances of "&nbsp " with " "
2.) remove all instances of "line start<P></P>line end"
3.) remove all instances of "line start<P>&nbsp;</P>line end"
4.) replace all instances of "line start<P></P><article>line end" with "<article>"

This is to clean up data. How can I do all of this in once hit?

Regards,

Neil.
Link to comment
https://forums.phpfreaks.com/topic/10032-search-and-replace-for/#findComment-37276
Share on other sites

[code]
<?php
$string = <<<LLL
jdsdssdsd <br>
jdhjdshj&nbsp;dsjdhsj <br />
<p></p>
<p>&nbsp;</p>
<p></p><article>
<br>
<br>
<p></p>
LLL;
$match = array();
$replace = array();

$match[] = '#<br>#i';
$replace[] = '<br />';

$match[] = '#^<p>&nbsp;</p>(?:\r?\n|\Z)#im';
$replace[] = '';

/**
* this has to be added to the array
* after the <p>&nbsp;</p>
*/

$match[] = '#&nbsp;#i';
$replace[] = ' ';

$match[] = '#^<p></p>(\r?\n|\Z)#im';
$replace[] = '';

$match[] = '#^<p></p><article>$#im';
$replace[] = '<article>';
print preg_replace($match, $replace, $string);

?>
[/code]
output
[code]
jdsdssdsd <br />
jdhjdshj dsjdhsj <br />
<article>
<br />
<br />
[/code]
Link to comment
https://forums.phpfreaks.com/topic/10032-search-and-replace-for/#findComment-37341
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.