regular expression
| From: | php@aeroinc.net | Date: | Fri, 06 Oct 2000 20:21:28 +0000 |
| Subject: | regular expression | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-18970@lists.php.net to get a copy of this message | ||
I need to do a find & replace, and have been unable to get one part of it working.
Let's say I have the text:
line 1
line 2
[code]
line a
line b
[/code]
line 3
It should be converted into:
line 1<br>
line 2<br>
<pre>
line a
line b
</pre>
line 3<br>
I want to convert all new lines to <br>'s, unless it falls within the [code] tag. This
for a forum that allows people to use a pseudo html language to markup their posts. The [code] tages
are translated to <pre>'s, but Netscape then double spaces, because the <pre> (by
nature) treats the new line as a <br> and then the nl2br() function adds a <br> as well.
It works fine on IE, but I'd rather it look proper on all browsers.
So in summary I need a regular expression (perl comaptible) that will replace all new lines to
"<br>", unless they fall in between "[code][/code]" tags. Or, a regular
expression that would remove all newlines between "[code]" tags would work as well.
I'm creating an array of expressions using the perl compatible. Looks somewhat like this:
$search = array (
'/\[b\](.+)\[\/b\]/',
'/\[i\](.+)\[\/i\]/',
'/\[u\](.+)\[\/u\]/',
'/\[url\](.+)\[\/url\]/',
'/\[url (.+)\](.+)\[\/url\]/',
'/\[code\](.+)\[\/code\]/s');
$replace = array (
'<b>\1</b>',
'<i>\1</i>',
'<u>\1</u>',
'<a href="\1" target="_blank"
class="lightbg">\1</a>',
'<a href="\1" target="_blank"
class="lightbg">\2</a>',
'<pre>\1</pre>');
--
Matthew Leverton - matthew@aeroinc.net