I first used trim () rtrim () ltrim () to delete spaces.
| The code is as follows: |
Copy code |
<? Php Echo trim ("blank"). "<br> "; Echo rtrim ("blank box"). "<br> "; Echo ltrim ("space"). "<br> "; ?> |
As a result, we did not want to delete any space in the middle. Later, we carefully checked the usage of the three functions.
Trim () removes spaces at both ends of a string,
Rtrim () removes the right space of a string,
Ltrim () is to remove the left space of a string.
It is clear that there is no ability to delete spaces in the middle. What should we do with Baidu? Some people have said that we should replace the function with regular expressions. Let's just give it a try.
Someone said this.
| The code is as follows: |
Copy code |
$ Str = "This line contains \ tliberal \ r \ n use of whitespace. \ n "; $ Str = trim ($ str); // first remove the leading and trailing spaces $ Str = preg_replace ('/\ s (? = \ S)/', '', $ str); // remove the two or more spaces $ Str = preg_replace ('/[\ n \ r \ t]/', ', $ str); // replace a non-space with a space. |
In the above example, we can remove all unnecessary spaces ..