Re: regular expression
| From: | Joe Stump | Date: | Fri, 06 Oct 2000 21:58:59 +0000 |
| Subject: | Re: regular expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18991@lists.php.net to get a copy of this message | ||
I would do this
$foo = str_replace('[code]','<code>',$var);
$foo = str_replace('[/code]','</code>',$var);
$foo = eregi_replace("\n",'<br>',$var); // or nl2br()
--Joe
On Fri, Oct 06, 2000 at 03:21:28PM -0500, php@aeroinc.net wrote:
> 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
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
/*****************************\
* Joe Stump *
* www.Care2.com *
* Office: 650.328.0198 *
* Extension: 122 *
\*****************************/
http://www.miester.org/Joe/Stump/Joe_Stump_resume.html
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GB/E/IT d- s++:++ a? C++++ UL++$ P+ L+++$ E----! W+++$
N+@ o? K? w---! O-@ M+@ V-! P(++) PE(+) Y+@ PGP+++@ t+@
5? R-! tv@ b+ DI++@ D(++++) G++@ e+@ h@ r+! z(+++++**)!
------END GEEK CODE BLOCK------