note 75002 added to function.preg-match-all
| From: | mike at php-webdesign dot nl | Date: | Tue, 08 May 2007 07:38:06 +0000 |
| Subject: | note 75002 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126315@lists.php.net to get a copy of this message | ||
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 ;-)
----
Server IP: 83.137.20.120
Probable Submitter: 80.255.240.169
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- https://master.php.net/note/edit/75002
Del: integrated -- https://master.php.net/note/delete/75002/integrated
Del: useless -- https://master.php.net/note/delete/75002/useless
Del: bad code -- https://master.php.net/note/delete/75002/bad+code
Del: spam -- https://master.php.net/note/delete/75002/spam
Del: non-english -- https://master.php.net/note/delete/75002/non-english
Del: in docs -- https://master.php.net/note/delete/75002/in+docs
Del: other reasons-- https://master.php.net/note/delete/75002
Reject -- https://master.php.net/note/reject/75002
Search -- https://master.php.net/manage/user-notes.php