Home > Backend Development > PHP Tutorial > Send a newly compiled violent version/gentle version of the Chinese interception function_PHP tutorial

Send a newly compiled violent version/gentle version of the Chinese interception function_PHP tutorial

WBOY
Release: 2016-07-20 11:00:03
Original
832 people have browsed it

It is recommended to use the violent version, which is safe and reliable; the gentle version is more efficient from a programming perspective. Haha

The basic principle is to correct the possible misalignment of off and len. The gentle version searches backward from off. The first character

/**
* @brief Simple and efficient string interception function (supports CJK characters)
*
* It simply determines the ASCII value of the high-order part and can handle most regular Chinese and English mixed strings
* Does not support 4-byte or 3-byte utf encoding
*
* Key points: Correct the misaligned off value/len value in double bytes (note the purpose of the default value of parameter $len is -1)
* The usage is the same as substr(), there may be problems with the low bits of GBK code (starting from 0x40)
*/
function my_substr($str, $off, $len = -1)
{
$mlen = strlen($str);

/* Step 0: Parameter security check and correction */
if ($off < 0)
$off = $mlen;
if ($off > $mlen)
$off = 0;

/* Step 1: $off correction, reverse search */
if ($off > 0)
{
$fix = $off;
$mb = false;
do
{
$ch = ord($str{$fix--});
if ($ch < 0x80)
break;
$mb = true;
}
while ($fix);

if ($mb)
{
$fix = ($off - $fix);
if ($fix & 1)
{
$off--;
$len ;
}
}
}

/* Step 2: $len correction, same as above */
if ($len <= 0 || ($len $off) >= $mlen)
{
$len = $mlen - $off;
}
else
{


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445551.htmlTechArticleIt is recommended to use the violent version, which is safe and reliable; the gentle version is more efficient from the perspective of programming. Haha Basic principles It corrects the possible misalignment of off and len. The gentle version searches backward from off...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template