PHP 2 ways to remove the leading and trailing spaces, PHP 2 types of Kinsoku
Seemingly simple question, in fact, there is a bit of a pit, the first place to escape the space, not a string, directly with trim () is not to go off.
1, replace with Preg_replace
Copy the Code code as follows:
$test = "Dfadad on the relationship between the Sanying and the responsible people 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 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
Copy the 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, 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. It is recommended to use UTF8 encoding.
http://www.bkjia.com/PHPjc/968269.html www.bkjia.com true http://www.bkjia.com/PHPjc/968269.html techarticle PHP 2 Ways to remove the leading and trailing spaces, PHP 2 seemingly very simple problem, in fact, a bit of a pit, the first place to escape the space, not a string, directly with trim () is not to go off ...