Home >Backend Development >PHP Problem >How to remove all spaces in php

How to remove all spaces in php

藏色散人
藏色散人Original
2021-02-25 09:49:108556browse

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.

How to remove all spaces in php

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!

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