It seems to be a very simple problem, but it is actually a bit tricky. First of all, space escaping here, It is not a string and cannot be removed directly using trim().
1, replace
with preg_replace
Copy code The code is as follows:
$test = " dfadad On the relationship between the responsible people and the three British 775fd ";
$test = preg_replace('/^( |s)*|( |s)*$/', '', $test);
var_dump($test);
//The results are as follows:
string 'dfadad On the relationship between the responsible people and the three British 775fd' (length=35)
This method was provided by friends in the group, and I would like to express my gratitude. This method is recommended because it is universal
2, trim method
Copy code The code is 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 found in the official manual of PHP. If it is UTF8 encoding, there is no problem in using this method. If GBK, GB2312, garbled characters will appear. There is also json_encode. If this function is gbk or gb2312, Chinese characters will be replaced by null. It is recommended to use utf8 encoding.