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

php remove spaces from string

藏色散人
藏色散人Original
2020-09-29 10:09:3813547browse

php method to remove spaces in a string: 1. Use the php function trim to remove; 2. Use the php function str_replace to remove; 3. Use the php function strtr to remove; 4. Use the trimall method to remove; 5. Use regular Remove normal spaces, etc.

php remove spaces from string

Recommended: "PHP Video Tutorial"

Removing spaces from strings in php

1. Use the php function trim():

 trim($str)

to remove ordinary spaces on both sides of the string;

2. Use the php function: str_replace():

str_replace(' ','',$str)

3. Use the php function: strtr():

strtr($str,array(' '=>''))

4. Use the encapsulation function you wrote:

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

5. Use this regular expression to remove ordinary spaces:

preg_replace('# #', '', $str)

6. Use this regular expression to remove all spaces including full-width spaces:

$str =preg_replace("/\s| /","",$str);

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
Previous article:php php_eol usageNext article:php php_eol usage