note 55804 added to function.nl2br
| From: | webmaster at cafe-clope dot net | Date: | Sat, 13 Aug 2005 22:50:33 +0000 |
| Subject: | note 55804 added to function.nl2br | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93569@lists.php.net to get a copy of this message | ||
based on previous notes, a generalist function that works with any <tag>...</tag>, and
its ^-1. (use it with "li", for example) :
<?php
function nl2any($string, $tag = 'p', $feed = '') {
// making tags
$start_tag = "<$tag" . ($feed ? ' '.$feed : '') .
'>' ;
$end_tag = "</$tag>" ;
// exploding string to lines
$lines = preg_split('
[\n\r]+', trim($string)) ;
// making new string
$string = '' ;
foreach($lines as $line)
$string .= "$start_tag$line$end_tag\n" ;
return $string ;
}
function any2nl($string, $tag = 'p') {
//exploding
preg_match_all("<".$tag."[^>]*>(.*)</".$tag.">Ui",
$string, $results) ;
// reimploding without tags
return implode("\n", array_filter($results[1])) ;
}
?>
I just had a problem when trying "<$tag[^>]*>(.*)</$tag>Ui"
regexp string, I can't figure out why.
----
Manual Page -- http://www.php.net/manual/en/function.nl2br.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55804
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55804&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55804&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55804&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55804&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55804&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55804&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55804&report=yes
Search -- http://master.php.net/manage/user-notes.php