Home >Backend Development >PHP Problem >How to clear spaces in php

How to clear spaces in php

藏色散人
藏色散人Original
2020-07-10 13:33:444715browse

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.

How to clear spaces in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn