Seemingly very simple problem, in fact, is still a bit of a pit, first of all, here is the space escape, not a string, directly with the trim () is not go away.
1, replace with Preg_replace
Copy Code code as follows:
$test = "Dfadad on the relationship between the Sanying and the People" 775FD ";
$test = Preg_replace ('/^ (|\s) *| (|\s) *$/', ', $test);
Var_dump ($test);
The results are as follows:
String ' Dfadad on the relationship between the people and the Sanying 775fd ' (length=35)
This method is provided by the small partners within the group and is appreciated here. This method is recommended for generality
2,trim method
Copy Code code as follows:
$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 to find, if it is UTF8 code, with this method, no problem. If gbk,gb2312, there will be garbled. There is also the Json_encode, this function if it is gbk,gb2312, the Chinese character will be replaced with NULL. It is recommended to use UTF8 encoding.