note 42952 modified in function.nl2br by aidan
| From: | aidan@php.net | Date: | Sun, 20 Jun 2004 09:10:58 +0000 |
| Subject: | note 42952 modified in function.nl2br by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71397@lists.php.net to get a copy of this message | ||
Here is a variant of nl2br that does not insert newlines inside HTML tags.
<?php
function www_nl2br ($testo)
{
# Example of preg_match_all's result:
# Array (
# [0] => Array (
# [0] => First line
# [1] => See also <a NEWLINE
# href="http://www.polimi.it/">www.polimi.it</a>
# [2] => Third line <a href="http://www.polimi.it/">www.polimi.it</a>
# )
# )
#
# The output then is
# First line<br/>
# See also <a
# href="http://www.polimi.it/">www.polimi.it</a><br/>
# Third line <a href="http://www.polimi.it/">www.polimi.it</a><br/>
preg_match_all ('/
(?>
(?> [^<\n]*) # testo fuori dai tag
(?> <[^>]*>)* # e tag se serve
)* # ad libitum
(\n|\Z) # fino a fine linea
/xs', $testo, $result);
return implode ("\n<br/>", $result[0]);
}
?>
--was--
Here is a variant of nl2br that does not insert newlines inside HTML tags.
function www_nl2br ($testo)
{
# Example of preg_match_all's result:
# Array (
# [0] => Array (
# [0] => First line
# [1] => See also <a NEWLINE
# href="http://www.polimi.it/">www.polimi.it</a>
# [2] => Third line <a href="http://www.polimi.it/">www.polimi.it</a>
# )
# )
#
# The output then is
# First line<br/>
# See also <a
# href="http://www.polimi.it/">www.polimi.it</a><br/>
# Third line <a href="http://www.polimi.it/">www.polimi.it</a><br/>
preg_match_all ('/
(?>
(?> [^<\n]*) # testo fuori dai tag
(?> <[^>]*>)* # e tag se serve
)* # ad libitum
(\n|\Z) # fino a fine linea
/xs', $testo, $result);
return implode ("\n<br/>", $result[0]);
}
http://php.net/manual/en/function.nl2br.php