note 75002 deleted from function.preg-match-all by tularis
| From: | tularis@php.net | Date: | Tue, 08 May 2007 08:55:38 +0000 |
| Subject: | note 75002 deleted from function.preg-match-all by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126321@lists.php.net to get a copy of this message | ||
Note Submitter: mike at php-webdesign dot nl
----
Not sure were to post this function, but this looked a nice place.
This function takes 2 arguments, $text and $length. It is meaned for people who want to create a
readmore link with eg. substr in a HTML field.
So, when you do substr($text, 0, 200) and a <b> tag is opend in the first 200 characters, and
closed in the 400th, the rest of the page is bold also. That is what this function takes care of:
<?
# still need some tweaking (eg. non closable tags)
# the return value is a string with all the close tags.
function readMore_closedtags($text, $length) {
preg_match_all('/<(.*?)>/s',substr(stripslashes($text), 0, $length),$out);
foreach($out[1] as $key2 => $val2){
if(
!preg_match("/br.*/", $val2) &&
!empty($val2)
){
$val2_arr = explode(" ", $val2);
$val2 = $val2_arr[0];
if(preg_match("/^\//", $val2))
$html_arr_close[] = strtolower($val2);
else
$html_arr_open[] = strtolower($val2);
}
}
$not_closed_tags = array();
foreach($html_arr_open as $tag){
$key = array_search("/" . $tag, $html_arr_close);
if($key !== false){
unset($html_arr_close[$key]);
} else {
$not_closed_tags[] = $tag;
}
}
$closed_tags_str = '';
foreach($not_closed_tags as $tag){
$closed_tags_str .= "</" . $tag . ">";
}
return $closed_tags_str;
}
?>
Hope someone finds it usefell ;-)