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
Share on other sites


How can I search on a line start and a line end?

I've got the following to replace the "<BR>" tags with "<BR />" but how can I remove a line such as:

line start<P>&nbsp;</P>line end

Regards,

Neil.
Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.