How to judge the length of Chinese and English mixed strings in PHP and how to capture examples

Source: Internet
Author: User
Tags ord
This article mainly introduces PHP for the Chinese and English mixed string length judgment and interception method, combined with an example of PHP in English string traversal, conversion, interception, calculation and other related operations skills, the need for friends can refer to the next

In this paper, we describe the method of PHP mixed string length judgment and interception in English and Chinese. Share to everyone for your reference, as follows:

/** * * Chinese  -English mixed string length judgment  * @param unknown_type $str * @param unknown_type $charset */function strlength ($str, $charset = ' Utf-8 ') {  if ($charset = = ' Utf-8 ')    $str = iconv (' utf-8 ', ' gb2312 ', $str);  $num = strlen ($STR);  $cnNum = 0;  for ($i = 0; $i < $num; $i + +) {    if (Ord (substr ($STR, $i + 1, 1)) > 127) {      $cnNum + +);      $i + +;    }  }  $enNum = $num-($cnNum * 2);  $number = ($enNum/2) + $cnNum;  return Ceil ($number);}
/** * Chinese-English mixed string interception * @param unknown_type $sourcestr * @param unknown_type $cutlength */function cut_str ($sourcestr, $cutl  Ength) {$returnstr = ';  $i = 0;  $n = 0; $str _length = strlen ($SOURCESTR);    The number of bytes in the string while (($n < $cutlength) and ($i <= $str _length)) {$temp _str = substr ($sourcestr, $i, 1); $ascnum = Ord ($temp _str);  Get the ASCII code of the $i character in the string if ($ascnum >= 224)//If the ASCII bit is high with 224, {$returnstr = $returnstr. substr ($sourcestr, $i, 3); According to the UTF-8 encoding specification, 3 consecutive characters are counted to a single character $i = $i + 3; The actual byte count is 3 $n + +; String length meter 1} elseif ($ascnum >= 192)//If the ASCII bit is high with 192, {$returnstr = $returnstr. substr ($sourcestr, $i, 2) ; According to the UTF-8 encoding specification, 2 consecutive characters are counted to a single character $i = $i + 2; The actual byte count is 2 $n + +; String length meter 1} elseif ($ascnum >= && $ascnum <= 90)//if uppercase, {$returnstr = $returnstr. substr      ($sourcestr, $i, 1); $i = $i + 1; The actual byte count still counts 1 $n + +; But considering overall aesthetics, uppercase letters are counted as a high-bit character} else//In other cases, including lowercase and half-width punctuation, {$returnstr = $returnstr. substr ($sourcestr, $i, 1); $i = $i + 1; The actual byte count is 1 $n = $n + 0.5;  Lowercase letters and half-width punctuation with half-high characters justifies ...} } if ($str _length > $cutlength) {$returnstr = $returnstr. "..."; Add an ellipsis at the end of the length to return $returnstr;}
Related Article

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.