Jump to content

Archived

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

nfr

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

Recommended Posts


Hello -

How can I replace all instances of "<br>" with "<br />" in a block of text? The "<br>" seems to be crashing my XML feed because there is no "</br>".

Thanks and regards,

Neil.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Look at the functions [a href=\"http://www.php.net/str_replace\" target=\"_blank\"]str_replace()[/a] and [a href=\"http://www.php.net/strip_tags\" target=\"_blank\"]strip_tags()[/a].

Ken

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
For that you need to use the regular expression functions. I really can't help you there, since I still can't understand them myself.

Ken

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.