note 20584 deleted from function.substr by aidan
| From: | aidan@php.net | Date: | Sat, 03 Jul 2004 07:56:02 +0000 |
| Subject: | note 20584 deleted from function.substr by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72389@lists.php.net to get a copy of this message | ||
Note Submitter: ken@wisers.com
----
When using substr() on the double byte character, for example chinese character, there may be a
occasion that trailing character is first byte of the double byte, it may crash you program.
E.g substr("¤@¤G¤T¥|", 0, 5) => ¤@¤G?
if you do echo (substr("¤@¤G¤T¥|", 0, 5) . "x" ) => "¤@¤G¤x"
, your "x" will disapper and became an chinese character.
The following function can be used to replace substr to handle these case:
function dbyte_substr($str, $start, $len=''){
if($len == ''){
$outstr = substr($str, $start);
}else{
$outstr = substr($str, $start, $len);
// Check the end bound is an double byte first byte or not
if(preg_match("/[\x80-\xFF]$/", $outstr)){
$outstr = substr("$outstr", 0, -1);
}
}
return $outstr;
}