Home  >  Article  >  Backend Development  >  How to remove all spaces from string in php

How to remove all spaces from string in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-19 09:29:544360browse

php method to remove all spaces in a string: first trim can only remove spaces on both sides of the string; then expand the trim function, the code is [function trimall($str), $oldchar=array(" ", " ","\t","\n","\r")].

How to remove all spaces from string in php

php method to remove all spaces in a string:

php removes all spaces in a string, actually Extension of the trim function, trim can only delete spaces on both sides of the string

The code is as follows:

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

Test:

$str = " a b c d e f ";
echo trimall($str);

Output result:

abcdef

Related learning recommendations: php programming (video)

The above is the detailed content of How to remove all spaces from 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