Seemingly simple question, actually still a bit of pit, first here space escape, not the string, directly with trim () is not to go off.
1, replace with Preg_replace
View copy print?
- $test = " dfadad on the 775fd of the relationship between the Sanying and the responsible people";
- $test = preg_replace ('/^ ( |\s) *| ( |\s) *$/', ', $test);
- Var_dump ($test);
- The results are as follows:
- String ' dfadad on the relationship between the responsible people and Sanying 775FD ' (length=35)
This method is provided by the small partners inside the group, which expresses thanks. Recommend this method, with versatility
2,trim method
View copy print?
- $test = " dfadad 3333adf775fd ";
- $test = Trim (Html_entity_decode ($test),chr (0xc2). Chr (0xa0));
- Var_dump ($test);
- The results are as follows:
- String ' Dfadad 3333adf775fd ' (length=19)
This method is from the official PHP manual, if it is UTF8 code, using this method, there is no problem. If gbk,gb2312, it will appear garbled. There is also the json_encode, if this function is gbk,gb2312, the Chinese characters will be replaced with NULL. Recommended encoding for the use of UTF8
PHP 2 ways to turn the trailing blanks