PHP Chinese string intercept

Source: Internet
Author: User

in PHP mb_substr () function is used to intercept the Chinese and English functions, you can quickly solve the problem of intercepting the length of the specified characters, let me introduce to you.

Hint: MB_SUBSTR is in PHP is not supported by default we need to find php.ini in Windows directory Open edit, search Mbstring.dll, find; Extension=php_mbstring.dll put the front; You can use the MB_SUBSTR function if the number is removed.

Function:mb_substr ($str, $start, $length, $encoding)

$str, the string that needs to be truncated
$start, where the truncation begins
$length, Length (note that this is different from Mb_strimwidth, 1 represents a Chinese character)
$encoding, code, I set it to Utf-8.

Example: Truncate article title, control in 15 words

The code is as follows Copy Code


$str = ' so that my string will not have garbled ^_^ ';

echo "MB_SUBSTR:". Mb_substr ($str, 0, 7, ' utf-8 ');
Result: so that my word
echo "
";

echo "Mb_strcut:". Mb_strcut ($str, 0, 6, ' utf-8 ');
Result: this
?>

The summary appeared he was able to solve the interception of Chinese characters OH

PHP Chinese string intercept

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.