Remove the PHP regular code with Chinese Spaces

Source: Internet
Author: User

There are several kinds of Chinese spaces: There is no simple solution to the problem, such as halfwidth and fullwidth spaces. For example, the Section characters are displayed as blank and people may misunderstand that it is a space, therefore, before removing spaces, you must first check whether your space is used. I will analyze the two solutions below.

(1)/[\ s |] +/. Note | the backend is followed by a fullwidth space.

(2) try to replace the Regular Expression in MB.

In addition, when using Unicode, add the regular expression descriptor u

However, there are many characters in Chinese that are displayed as spaces. For example, the Unicode code of this character is c2a0, and the following expression can be used to solve the problem.

$ New ['content'] = preg_replace ('/^ [(\ xc2 \ xa0) | \ s] +/', ', $ new ['content']);

Here is the details: \ xc2a0 is a Unicode character, but it cannot match multiple characters. Only one character can be replaced. [This may be because of the byte] Then I suddenly found such a writing method, solved the problem. In addition, in order to avoid mixing Chinese spaces and English spaces, an \ s is added to remove them.

In fact, regular expressions on Unicode are not very useful.

Other references:

During daily data processing, we often produce extra spaces. If you want to compare strings, this will lead to problems and waste additional storage space.

How do I remove spaces? Maybe you will first think of the PHP built-in function trim (). Yes, it does. However, in this case, it cannot handle the starting and ending parts of characters: Changing multiple spaces into one space, changing spaces into ordered queues, and so on...

Therefore, regular expressions are useful. Take a look at the followingCode:

$ STR = "this line contains \ tliberal \ r \ n use of whitespace. \ n ";

// First remove the leading and trailing Spaces
$ STR = trim ($ Str );

// Remove the two or more spaces
$ STR = preg_replace ('/\ s (? = \ S)/', '', $ Str );

// Replace a non-space with a space.
$ STR = preg_replace ('/[\ n \ r \ t]/', '', $ Str );

The preceding example can remove all unnecessary spaces. Use TRIM () to remove leading and trailing spaces, and then use preg_replace () to remove repeated spaces.
(? =) Indicates that only the following spaces are matched with the leading spaces.

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.