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

王林
Release: 2023-03-07 12:28:01
Original
2220 people have browsed it

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"
?>
Copy after login

2. Use str_replace function

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

3. Use encapsulation function

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

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!

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