note 47091 added to function.substr
| From: | kovacsendre at no_spam_thanks_kfhik dot hungary | Date: | Tue, 02 Nov 2004 15:38:45 +0000 |
| Subject: | note 47091 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79508@lists.php.net to get a copy of this message | ||
Here are the replacement functions for substr() and strlen() I use when support for html entities is
required:
<?php
function html_strlen($str) {
$chars = preg_split('/(&[^;\s]+;)|/', $str, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE);
return count($chars);
}
function html_substr($str, $start, $length = NULL) {
if ($length === 0) return ""; //stop wasting our time ;)
//check if we can simply use the built-in functions
if (strpos($str, '&') === false) { //No entities. Use built-in functions
if ($length === NULL)
return substr($str, $start);
else
return substr($str, $start, $length);
}
// create our array of characters and html entities
$chars = preg_split('/(&[^;\s]+;)|/', $str, -1, PREG_SPLIT_NO_EMPTY |
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
$html_length = count($chars);
// check if we can predict the return value and save some processing time
if (
($html_length === 0) /* input string was empty */ or
($start >= $html_length) /* $start is longer than the input string */ or
(isset($length) and ($length <= -$html_length)) /* all characters would be omitted */
)
return "";
//calculate start position
if ($start >= 0) {
$real_start = $chars[$start][1];
} else { //start'th character from the end of string
$start = max($start,-$html_length);
$real_start = $chars[$html_length+$start][1];
}
if (!isset($length)) // no $length argument passed, return all remaining characters
return substr($str, $real_start);
else if ($length > 0) { // copy $length chars
if ($start+$length >= $html_length) { // return all remaining characters
return substr($str, $real_start);
} else { //return $length characters
return substr($str, $real_start, $chars[max($start,0)+$length][1] - $real_start);
}
} else { //negative $length. Omit $length characters from end
return substr($str, $real_start, $chars[$html_length+$length][1] - $real_start);
}
}
?>
Example:
html_substr("ábla6bla", 1, 4) -> "bla6"
If you happen to find any bugs, please let me know.
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47091
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47091&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47091&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47091&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47091&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47091&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47091&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47091&report=yes
Search -- http://master.php.net/manage/user-notes.php