Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 nfr

nfr
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 19 May 2006 - 08:19 PM


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.

#2 nfr

nfr
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 19 May 2006 - 08:46 PM


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.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 May 2006 - 09:13 PM

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



#4 nfr

nfr
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 19 May 2006 - 10:17 PM


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.

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 May 2006 - 11:30 PM

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

#6 nfr

nfr
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 20 May 2006 - 12:23 AM


Anyone?

Thanks,

Neil.

#7 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 20 May 2006 - 01:20 AM

<?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);

?>
output
jdsdssdsd <br />
jdhjdshj dsjdhsj <br />
<article>
<br />
<br />





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users