Home > Backend Development > PHP Problem > php remove spaces from string

php remove spaces from string

藏色散人
Release: 2023-03-06 10:54:01
Original
1895 people have browsed it

php method to remove spaces in a string: first create a PHP sample file; then delete all spaces in the specified string through the "str_replace(' ', '', ' a bab ');" method Can.

php remove spaces from string

Recommended: "PHP Video Tutorial"

Remove all spaces in the string

<?php
//删除所有空格
echo str_replace(&#39; &#39;, &#39;&#39;, &#39; a bab  &#39;);echo "\n";
echo preg_replace(&#39;/ /&#39;, &#39;&#39;, &#39;   ab  ab  &#39;);echo "\n";
echo strtr(&#39;    ab    ab    &#39;, array(&#39; &#39;=>&#39;&#39;));echo "\n";
function trimall($str)//删除所有的空格
{
    $qian=array(" "," ","\t","\n","\r");
    $hou=array("","","","","");
    return str_replace($qian,$hou,$str); 
}
echo trimall(" a  v  ");
echo "\n";
?>
Copy after login

The result is as shown:

php remove spaces from string

The above is the detailed content of php remove spaces from string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template