Home >Backend Development >PHP Problem >What are the functions to delete blank spaces in php?

What are the functions to delete blank spaces in php?

藏色散人
藏色散人Original
2021-06-02 09:41:341683browse

php functions to delete blank spaces are: 1. Use the "str_replace()" function to delete; 2. Use the "strtr()" function to delete; 3. Use the encapsulated function "trimall" to delete.

What are the functions to delete blank spaces in php?

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php What are the functions for deleting blank spaces? 3 ways to delete spaces in the middle of strings in php

The first one: use str_replace() function

The code is as follows:

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

The second one: use strtr() The code of function

is as follows:

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

strtr() function is a bit special to use. In essence:

The code is as follows:

<?php
strtr(&#39;ewb&#39;, &#39;web&#39;, &#39;123&#39;) ==
strtr(&#39;ewb&#39;, array(&#39;e &#39;=> &#39;2&#39;, &#39;w&#39; => &#39;1&#39;, &#39;b&#39; => &#39;3&#39;)) ==
str_replace(array(&#39;e&#39;, &#39;w&#39;, &#39;b&#39;), array(&#39;2&#39;, &#39;1&#39;, &#39;3&#39;), &#39;ewb&#39;);
?>

Third method: use The code of the encapsulated function

is as follows:

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

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the functions to delete blank spaces 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