This seems to be a very simple problem, but it is actually a bit tricky. First of all, the spaces are escaped here, not strings, and cannot be removed directly using trim().
1, replace
with preg_replace
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
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.
http://www.bkjia.com/PHPjc/968658.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/968658.htmlTechArticle2 ways to remove leading and trailing spaces in php. This article mainly introduces 2 ways to remove leading and trailing spaces in php. ,This article gives two methods of using preg_replace substitution and trim function and gives examples. Required...