Home >Backend Development >PHP Problem >How to remove all spaces in php
php method to remove all spaces: first create a PHP sample file; then use the "trim($str);" method to remove leading and trailing spaces; finally use the "preg_replace" expression to remove all spaces.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
php To remove spaces, you need to replace them with strings Functions and regular replacement functions
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 non-spaces with a space
Use the above example to remove all extra 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.
[Recommended learning: "PHP Video Tutorial"]
The above is the detailed content of How to remove all spaces in php. For more information, please follow other related articles on the PHP Chinese website!