This article mainly introduces two methods for removing spaces at the beginning and end of php. This article provides two methods, preg_replace and trim functions, and provides examples, if you need a friend, you can refer to the seemingly simple question. In fact, it is still a bit pitfall. First of all, space escape is not a string. trim () cannot be used directly.
1. Replace with preg_replace
The Code is as follows:
$ Test = "dfadad's relationship between the responsible people and sanying 775fd ";
$ Test = preg_replace ('/^ (| \ s) * | (| \ s) * $/', '', $ test );
Var_dump ($ test );
// The result is as follows:
String 'dfadad on the relationship between the responsible persons and sanying 775fd '(length = 35)
This method is provided by friends in the group, and I would like to express my gratitude. This method is recommended and 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 result is as follows:
String 'dfadad 3333adf775fd '(length = 19)
This method is found in the official php manual. If it is UTF-8 encoded, it is okay to use this method. If it is GBK or GB2312, garbled characters will appear. There is also json_encode. If this function is gbk or gb2312, Chinese characters will be replaced with null. Utf8 encoding is recommended.