note 97349 added to function.substr
| From: | kueb at NOSPAM dot gmx dot de | Date: | Thu, 15 Apr 2010 12:50:31 +0000 |
| Subject: | note 97349 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168266@lists.php.net to get a copy of this message | ||
I had some problems with the order of the closing of tags.
Example:
<p>some <b><i>very</i>long</b> text to show my problem</p>
would be converted to (if the script would cut the part after "very":
<p>some <b><i>very</p></b></i>
My second problem was, that the script didn't recognized tags like <a href="#"
title="title">
So my version based on bennys and erez' work looks like this:
<?php
function mb_substrws($text, $length = 180) {
if((mb_strlen($text) > $length)) {
$whitespaceposition = mb_strpos($text, ' ', $length) - 1;
if($whitespaceposition > 0) {
$chars = count_chars(mb_substr($text, 0, ($whitespaceposition + 1)), 1);
if ($chars[ord('<')] > $chars[ord('>')]) {
$whitespaceposition = mb_strpos($text, ">", $whitespaceposition) - 1;
}
$text = mb_substr($text, 0, ($whitespaceposition + 1));
}
// close unclosed html tags
if(preg_match_all("|(<([\w]+)[^>]*>)|", $text, $aBuffer)) {
if(!empty($aBuffer[1])) {
preg_match_all("|</([a-zA-Z]+)>|", $text, $aBuffer2);
if(count($aBuffer[2]) != count($aBuffer2[1])) {
$closing_tags = array_diff($aBuffer[2], $aBuffer2[1]);
$closing_tags = array_reverse($closing_tags);
foreach($closing_tags as $tag) {
$text .= '</'.$tag.'>';
}
}
}
}
}
return $text;
}
----
Server IP: 217.160.235.204
Probable Submitter: 193.24.33.227
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/97349
Del: integrated -- https://master.php.net/note/delete/97349/integrated
Del: useless -- https://master.php.net/note/delete/97349/useless
Del: bad code -- https://master.php.net/note/delete/97349/bad+code
Del: spam -- https://master.php.net/note/delete/97349/spam
Del: non-english -- https://master.php.net/note/delete/97349/non-english
Del: in docs -- https://master.php.net/note/delete/97349/in+docs
Del: other reasons-- https://master.php.net/note/delete/97349
Reject -- https://master.php.net/note/reject/97349
Search -- https://master.php.net/manage/user-notes.php