note 55804 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:11:45 +0000 |
| Subject: | note 55804 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195188@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at cafe-clope dot net
----
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.