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

How to remove all spaces from string in php

coldplay.xixi
Release: 2023-03-04 18:20:02
Original
4447 people have browsed it

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);
}
Copy after login

Test:

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

Output result:

abcdef
Copy after login

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!

Related labels:
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