Home > Archive > PHP Language > April 2007 > Preg_replace problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Preg_replace problem
|
|
| matthew.davey@gmail.com 2007-04-26, 6:58 pm |
| Hi. Having a little problem with preg_replace in php.
I'm trying to convert newlines from a form input to HTML <br /> tags,
but havingnever used expression matching I can't seem to get it
working properly. I'm searching online as well but was wondering if
any of you guys could help.
I'm using this code at the moment:
$text=preg_replace('/\s/','<br>',$input);
$text=preg_replace('/<br><br>/i','<br />',$text);
($input is the input code to the function)
the problem with this is that is also converts spaces in <br /> which
is not very useful at al :(.
Any ideas how i can convert all windows and linux newline characters /
r/n | /n into <br /> using preg_replace guys? I'm probably being
stupid but i can't seem to get it woring :(
| |
|
| matthew.davey@gmail.com wrote:
> Hi. Having a little problem with preg_replace in php.
>
> I'm trying to convert newlines from a form input to HTML <br /> tags,
> but havingnever used expression matching I can't seem to get it
> working properly. I'm searching online as well but was wondering if
> any of you guys could help.
>
> I'm using this code at the moment:
>
> $text=preg_replace('/\s/','<br>',$input);
> $text=preg_replace('/<br><br>/i','<br />',$text);
>
> ($input is the input code to the function)
>
> the problem with this is that is also converts spaces in <br /> which
> is not very useful at al :(.
>
> Any ideas how i can convert all windows and linux newline characters /
> r/n | /n into <br /> using preg_replace guys? I'm probably being
> stupid but i can't seem to get it woring :(
nl2br() would seem the logical solution...
If you're curious what the regex should be:
$text = preg_replace('/(\r\n|\n|\r)/','<br />',$text);
--
Rik Wasmus
Estimated date being able to walk again: 01-05-2007.
Less then a w , hurray!
| |
| Michiel 2007-04-26, 9:57 pm |
| Why not:
function nl2br2($string) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $string;
}
On 26 Apr 2007 10:20:29 -0700, matthew.davey@gmail.com wrote:
>Hi. Having a little problem with preg_replace in php.
>
>I'm trying to convert newlines from a form input to HTML <br /> tags,
>but havingnever used expression matching I can't seem to get it
>working properly. I'm searching online as well but was wondering if
>any of you guys could help.
>
>I'm using this code at the moment:
>
> $text=preg_replace('/\s/','<br>',$input);
> $text=preg_replace('/<br><br>/i','<br />',$text);
>
>($input is the input code to the function)
>
>the problem with this is that is also converts spaces in <br /> which
>is not very useful at al :(.
>
>Any ideas how i can convert all windows and linux newline characters /
>r/n | /n into <br /> using preg_replace guys? I'm probably being
>stupid but i can't seem to get it woring :(
|
|
|
|
|