How to remove all spaces in string in php

青灯夜游
Release: 2023-03-15 10:44:01
Original
9858 people have browsed it

php method to remove all spaces in a string: 1. Use str_replace() function, syntax "str_replace(" ","",$str)"; 2. Use preg_replace() function, syntax "preg_replace ('/\s /','',$str)".

How to remove all spaces in string in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php removes all strings Space method

  • Use str_replace() function to remove all spaces

  • Use preg_replace() function to remove all spaces

1. Use str_replace() function

We use the built-in function str_replace() to replace substrings in strings or arrays. The replaced string is passed as a parameter. The correct syntax for using this function is as follows.

str_replace($searchString, $replaceString, $originalString, $count);
Copy after login

The built-in function str_replace() has four parameters. Its detailed parameters are as follows

OptionalIt tells the total number of substitutions made to
Parameter
Description
$searchStringMandatoryIt is the substring or array we want to find and replace
$replaceStringMandatory It is the string we want to place in the position of $searchString. The function will check for occurrences of $searchString and replace it with $replaceString. This function will check for an occurrence of $searchString and replace it with $replaceString. It can also be an array.
$originalStringforce It is the original string from which we want to find a substring or a characters to replace.
##$count$originalString Times
This function returns the final string after all substitutions.

The program below shows how we can remove all spaces from a given string using str_replace() function.

<?php 
header("Content-type:text/html;charset=utf-8");
$searchString = " ";
$replaceString = "";
$originalString = "This is a programming tutorial"; 
 
$outputString = str_replace($searchString, $replaceString, $originalString); 
echo "原字符串:".$originalString."<br>";  
echo "去除空格后的字符串:".$outputString; 
  
?>
Copy after login

How to remove all spaces in string in php

We passed a space character as $searchString and an empty string as $replaceString. The output will be a string without spaces.

Now, if we pass in the $count parameter, the function will tell us the number of replacements for this string.

<?php 
header("Content-type:text/html;charset=utf-8");
$searchString = " ";
$replaceString = "";
$originalString = "This is a programming tutorial"; 
 
$outputString = str_replace($searchString, $replaceString, $originalString, $count); 
echo "原字符串:".$originalString."<br>";  
echo "去除空格后的字符串:".$outputString."<br>"; 
echo "替换字数:".$count; 
?>
Copy after login

How to remove all spaces in string in php

2. Use the preg_replace() function

You can also use the preg_replace() function to remove all spaces in the string . This function can remove not only space characters but also tab characters from a string. The correct syntax for using this function is as follows.

preg_replace($regexPattern, $replacementVar, $original, $limit, $count)
Copy after login

The function preg_replace() accepts five parameters. Its detailed parameters are as follows

ParametersDescription##$regexPattern$replacementVar$original$limit$countWe will use the pattern

forceIt is what we will put in the original string Or the pattern searched in an array
mandatory It is the string we use to replace the search value or array
forceIt is a string or array from which we want to find the value and Replace it.
OptionalThis parameter limits the number of replacements
OptionalThis parameter tells us the total number of times to replace the original string or array
/\s /

to find spaces. The procedure to remove spaces from a string is as follows. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;?php header(&quot;Content-type:text/html;charset=utf-8&quot;); $originalString = &quot;This is a programming tutorial&quot;; $outputString = preg_replace(&amp;#39;/\s+/&amp;#39;, &amp;#39;&amp;#39;, $originalString); echo &quot;原字符串:&quot;.$originalString.&quot;&lt;br&gt;&quot;; echo &quot;去除空格后的字符串:&quot;.$outputString.&quot;&lt;br&gt;&quot;; ?&gt;</pre><div class="contentsignin">Copy after login</div></div>

How to remove all spaces in string in phpWe know that the total number of substitutions for this string is 4, now we will limit the number of substitutions.

<?php 
header("Content-type:text/html;charset=utf-8");
$searchString = " ";
$replaceString = "";
$limit = 2;
$originalString = "This is a programming tutorial"; 
 
$outputString = preg_replace(&#39;/\s+/&#39;, &#39;&#39;, $originalString,$limit); 
echo "原字符串:".$originalString."<br>";  
echo "去除空格后的字符串:".$outputString."<br>"; 

?>
Copy after login

How to remove all spaces in string in php#It can be seen that it was only replaced twice.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to remove all spaces in 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!