Home >Backend Development >PHP Problem >How to clear spaces in php
php method to remove spaces: first use the "trim" function to remove leading and trailing spaces; then use "preg_replace" to remove more than two spaces; finally replace non-spaces with one space.
php To remove spaces, you need to use the string replacement function and the regular replacement function
Remove spaces in the string str_replace (' ','',$cat_name)
$str = ” This line contains\tliberal \r\n use of whitespace.\n\n”; $str = trim($str);// 首先去掉头尾空格 $str = preg_replace(’/\s(?=\s)/’, ‘’, $str);// 接着去掉两个空格以上的 $str = preg_replace(’/[\n\r\t]/’, ‘ ‘, $str);//
Finally replace the non-space with a space
Use the above example to remove all excess spaces.
First use TRim() to remove leading and trailing spaces,
Then use preg_replace() to remove repeated spaces. The (?=) in
means that only the following spaces and the preceding spaces will be matched.
For more related knowledge, please visit PHP Chinese website!
The above is the detailed content of How to clear spaces in php. For more information, please follow other related articles on the PHP Chinese website!