Home  >  Article  >  Backend Development  >  php remove spaces from string

php remove spaces from string

藏色散人
藏色散人Original
2020-09-30 10:00:191782browse

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";
?>

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!

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