PHP Intercept string length function detailed

Source: Internet
Author: User
Tags strlen

Common character-handling functions

The code is as follows Copy Code

Strstr (string,string) = STRCHR (,)//from the first occurrence of a string in the previous place to intercept to the last
STRRCHR (string,string)//From the last occurrence of a string to the end
Strpos (String,string[,int])//Where the first occurrence of a string
Strrpos (string,string)//Where the last occurrence of a string
SUBSTR (String,int[,int])//intercepts the string from the specified location, specifying the length of the interception.
Strlen (String)//Get length of string

Assume

$str = "This is 1 strings";
This string contains a half-width character that is also executed:

The code is as follows Copy Code
if (strlen ($STR) >10) $str =substr ($STR, 10);

Because the original string $str 10th, 11 characters constitute the Chinese character "character";
String segmentation will split the Chinese character, so that the intercepted string will find garbled phenomenon

Then we can calculate the string length first.

The code is as follows Copy Code

? Php
Header (' content-type:text/html; Charset=utf-8 ');
$str = "In the scholar-bureaucrat sdfsdfcxvzv a section";

$pa = '/[x{4e00}-x{9fa5}]/siu ';
Preg_match_all ($pa, $str, $r);

$count = count ($r [0]);
echo "In the current string, a total of $count characters";

if ($count >10)
{
If the number of Chinese characters is greater than 10, your code
}

?>

Add

PHP calculates the length of the string, including how PHP calculates the string length in English, GBK, UTF-8 multiple character sets. English string length
Strlen () is a function of PHP to compute the English string with its own.

The

gbk string length
character evaluates to 2 characters, and the English character evaluates to 1, which enables you to count the functions of the string length in Chinese. function Abslength ($str) {

  code is as follows copy code
$i = 0;
while ($i < $len)
{
       if (Preg_match/^[. chr (0XA1). " -". Chr (0xFF)."] +$/", $str [$i]))
       {
         $i +=2;
      }
       Else
       {
          $i +=1;
      }
}
return $i;
}

UTF8 string length
The STRLEN_UTF8 function defined below can be used to count the length of the UTF-8 string, but the function does not consider bytes, which is somewhat similar

The length method of a string in Javascript, in which all characters are computed at 1 lengths. <?php//Description: Calculates the length of the UTF-8 string (suddenly

Slightly-byte scheme)

The code is as follows Copy Code
function Strlen_utf8 ($STR) {
$i = 0;
$count = 0;
$len = strlen ($STR);
while ($i < $len) {
$CHR = Ord ($str [$i]);
$count + +;
$i + +;
if ($i >= $len) break;
if ($CHR & 0x80) {
$CHR <<= 1;
while ($CHR & 0x80) {
$i + +;
$CHR <<= 1;
}
}
}
return $count;
}
$STR = "Www.111cn.net-PHP information";
echo Strlen_utf8 ($STR);
?>

This allows you to accurately intercept your Chinese and English hybrid fonts, such as the example

The code is as follows Copy Code

Support Gb2312,gbk,utf-8,big5 Chinese interception method

/*

* Chinese interception, support Gb2312,gbk,utf-8,big5

*

* @param string $str to intercept

* @param int $start intercept start position

* @param int $length intercept length

* @param string $charset UTF-8|GB2312|GBK|BIG5 encoding

* @param $suffix whether to add a suffix

*/

Public Function Csubstr ($STR, $start =0, $length, $charset = "Utf-8", $suffix =true)

{

if (function_exists ("Mb_substr"))

{

if (Mb_strlen ($str, $charset) <= $length) return $str;

$slice = Mb_substr ($str, $start, $length, $charset);

}

Else

{

$re [' utf-8 '] = "/[x01-x7f]| [XC2-XDF] [x80-xbf]| [Xe0-xef] [X80-XBF] {2}| [Xf0-xff]

[X80-XBF] {3}/";

$re [' gb2312 '] = "/[x01-x7f]| [Xb0-xf7] [xa0-xfe]/];

$re [' gbk '] = "/[x01-x7f]| [X81-xfe] [x40-xfe]/];

$re [' big5 '] = "/[x01-x7f]| [X81-xfe] ([X40-x7e]|xa1-xfe]) /";

Preg_match_all ($re [$charset], $STR, $match);

if (count ($match [0]) <= $length) return $str;

$slice = Join ("", Array_slice ($match [0], $start, $length));

}

if ($suffix) return $slice. " ...";

return $slice;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.