Forum Moderators: coopster

Message Too Old, No Replies

How to find a newline in preg replace?

         

willybfriendly

4:27 am on Nov 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Has to be simple, but I am not getting this right.

So, how to I test for newline(s) in a preg_replace()?

Trying to clean up some poor code in a CMS. I have this, which works:


$pattern = '{<h1>[ *]<a href(.*)</a>[ *]</h1>}';
$replacement = '<h2><a href$1</a></h2>';
$string = '<h1><a href="http://localhost/folder/file.htm" class="class">Title</a></h1></td>';
$ntext=preg_replace($pattern, $replacement, $string);

Problem is that there can be an unknown number of spaces and/or newlines between <h1> and <a... and also between </a> and </h1>

I can test for the spaces, but have been unable to get the test for a newline to work. Have tried \n without success.

BTW, don't ask why I am doing this rather than hacking the CMS code. This really is an easier way on this one ;)

phparion

11:08 am on Nov 9, 2007 (gmt 0)

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



you can cover spaces and newlines etc with \s flag.