Forum Moderators: coopster

Message Too Old, No Replies

preg replace question

         

Mister_L

12:45 am on Nov 18, 2009 (gmt 0)

10+ Year Member



I have a long xml string that I want to display, but I want to add line breaks after closing tags.So I tried the following:

$replacement = '$1$2$3' . "<br>" ;

echo preg_replace("/(&lt;\/)([^&gt;]*)(&gt;)/s",$replacement,$xml_string);

It seems to be working for some of the closing tags, but for others not.Why is that?

Thanks.

[edited by: eelixduppy at 12:50 am (utc) on Nov. 18, 2009]
[edit reason] disabled smileys [/edit]

TheMadScientist

2:57 am on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't tested the following, but it's what I would start with:

$replacement = '$1' . "<br>" ;
echo preg_replace("#(</[^>]*>)#m",$replacement,$xml_string);

The biggest change I made is to the modifier, which makes it multiline (I'm actually not sure if you need it, but there's nothing to test with, and it shouldn't hurt).

Also, I'm not sure if you are needing to match the html values of the tags, but I made the expression a little more readable using the actual characters, as well as eliminated the unnecessary back-references.

If this does not work and you need some more help, please post a portion of the string (examplified if necessary) including line breaks and a few tags, so there is something to test with.