Home>Article>Backend Development> How to remove spaces in the middle of a string in php?

How to remove spaces in the middle of a string in php?

Christopher Nolan
Christopher Nolan Original
2020-07-22 13:31:22 2889browse

php method to delete spaces in the middle of a string: 1. Use the regular method, the code is [echo preg_replace('# #', '', 'ab ab')]; 2. Use [str_replace()] Function method, the code is [echo str_replace(' ')].

How to remove spaces in the middle of a string in php?

php method to delete spaces in the middle of a string:

First: use regular expressions

The code is as follows:

Second type: Use str_replace() function

The code is as follows:

Third method: Use the strtr() function

The code is as follows:

'')); // 输出 "abab" ?>

strtr()The use of the function is a bit special, in essence:

The code is as follows:

 '2', 'w' => '1', 'b' => '3')) == str_replace(array('e', 'w', 'b'), array('2', '1', '3'), 'ewb'); ?>

Fourth method: Use encapsulation function

The code is as follows:

function trimall($str)//删除空格 { $qian=array(" "," ","\t","\n","\r"); $hou=array("","","","",""); return str_replace($qian,$hou,$str); }

Related learning recommendations:PHP programming from entry to proficiency

The above is the detailed content of How to remove spaces in the middle of a string 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