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.