Php simply intercepts a mix of Chinese and English strings. only two lines of code are required! _ PHP Tutorial

Source: Internet
Author: User
Php simply intercepts a mix of Chinese and English strings. only two lines of code are required !. When talking about Chinese and English mixed sorting and truncation, we first think of ascii, hexadecimal, regular matching, and cyclic counting. Today, I will share with you the mb extension of php. it will teach you how to easily handle the counting and intercepting of Chinese and English mixed sorting, the first thing that comes to mind is ascii, hexadecimal, regular matching, and cyclic counting.

Today, I want to share with you how to easily process strings through the mb extension of php.

First, we will introduce the functions used:

Mb_strwidth ($ str, $ encoding) returns the string width.

$ Str string to be calculated

$ Encoding used by encoding, such as utf8 and gbk

Mb_strimwidth ($ str, $ start, $ width, $ tail, $ encoding) truncates a string by its width.

$ Str string to be truncated

$ Start: where to start the truncation. the default value is 0.

$ Width: the width to be truncated.

$ Tail appends the string to the string behind the truncated string, which is commonly used...

$ Encoding used by encoding

The following is an example:

 10) {// this parameter is set to start from 0 and take 10 append ..., use utf8 encoding // note the append... it will also be calculated to the length of $ str = mb_strimwidth ($ str, 0, 10 ,'... ', 'utf8');} // Finally output aaaa... 4 a, 4, 1, 2, 3, 3, 4, 2, 3, 3, 4, 2, 3, = 9 // Is it easy, some people have said why 9, not 10? // Because "ah" is followed by "ah", the number of Chinese characters is counted as 2. 9 + 2 = 11 has exceeded the setting. Therefore, if one character is removed, the value of echo $ str is 9;

I will introduce some other functions below:

Mb_strlen ($ str, $ encoding) returns the length of the string.

$ Str string to be calculated

$ Encoding used by encoding

Mb_substr ($ str, $ start, $ length, $ encoding) intercepts a string

$ Str string to be truncated

$ Start

$ Length

$ Encoding used by encoding

In fact, these two functions are very similar to strlen () and substr (). The only difference is that encoding can be set.

Bottom edge instance:

 

In fact, there are a lot of useful functions in the mb extension. I will not list them here.

If you are interested, you can view the official manual.

Http://www.php.net/manual/zh/ref.mbstring.php

Now, we will introduce you here today.

Bytes. Today, I want to share with you how to easily handle the mb extension of php...

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.