Php intercepts a Chinese character string. it intercepts the first occurrence of the given string and intercepts 100 characters.

Source: Internet
Author: User
Php intercepts a Chinese character string and intercepts it at the position where the first occurrence of the given string is made. it intercepts 100 characters. For example, you can use the following two methods to intercept and find that none of the extracted results are correct. $ Word is the string to be intercepted, and $ key_word is the given substring. Method 1: PHPcodemb php intercepts a Chinese string and intercepts it at the position where the given string appears for the first time, it contains 100 characters.
For example, you can use the following two methods to intercept and find that none of the extracted results are correct.
$ Word is the string to be intercepted, and $ key_word is the given substring.
Method 1:
PHP code
  mb_substr($word,strpos($word,$key_word)/3,100,'utf-8');


Method 2:
PHP code
  $start_key = mb_strpos($word,$key_word);$start_key = $start_key>0?$start_key:0;mb_substr($word,$start_key,100,'utf-8');


------ Solution --------------------
I found a very useful function, mb_strimwidth ($ str, 0, 80, '', 'utf8'), and width truncation
------ Solution --------------------
I am so sweaty. the code written by people who do not know the code really hurts.

Remember, strstr/strpos is used for ASCII strings, that is, 1-byte and 1-byte comparison. do not care about Encoding. for GBK/UTF8, it can also work normally under certain circumstances, because the single-byte GBK/UTF8 non-ASCII characters are all 1-bit, but the GBK code is prone to problems, because the combination of the first byte of two 2-byte characters may cause incorrect matching.

MB is the encoding function, so the numbers passed to it and returned by it are the number of characters, not the number of bytes.

You see that your first code uses strpos. if the utf8 code is okay, nothing else can be done to tell the truth. UTF8 is enough. you can also assume that all characters are 3 bytes... This is wrong.

The second code is much more reliable. Unfortunately, you didn't tell mb_strpos encoding, so it won't be finished.




------ Solution --------------------
This is not the case for mb_string function groups.

Mb_internal_encoding ("UTF-8 ");
Mb_substr ($ word, mb_strpos ($ word, $ key_word), 100 );
------ Solution --------------------
PHP code
// String truncation. all characters are 1 characters in length and are applicable to gbk and UTF-8. Function cut ($ str, $ len = 12, $ dot = '... ') {if (mb_strlen ($ str, "UTF-8") <= ($ len + 1) {$ str = $ str ;} else {$ str = mb_substr ($ str, 0, $ len, "UTF-8 "). $ dot;} return $ str ;}

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.