Home >Backend Development >PHP Problem >What are the ways to remove spaces in strings in php

What are the ways to remove spaces in strings in php

王林
王林Original
2020-11-06 10:17:242214browse

php methods to remove spaces in strings are: 1. Use regular expressions to remove spaces, such as [preg_replace('# #', '', 'ab ab')]; 2. Use the str_replace function Remove spaces; 3. Use the wrapper function to remove spaces.

What are the ways to remove spaces in strings in php

The method is as follows:

(Learning video recommendation: java course)

1. Regular Expression

<?php
echo preg_replace(&#39;# #&#39;, &#39;&#39;, &#39;ab     ab&#39;);
//输出 "abab"
?>

2. Use str_replace function

<?php
echo str_replace(&#39; &#39;, &#39;&#39;, &#39;ab    ab&#39;);
//输出 "abab&#39;
?>

3. Use encapsulation function

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

Related recommendations:php training

The above is the detailed content of What are the ways to remove spaces in strings 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