Home >Backend Development >PHP Problem >How to remove all spaces in string in php
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)".
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);
The built-in function str_replace() has four parameters. Its detailed parameters are as follows
Parameter | Description | |
---|---|---|
$searchString |
Mandatory | It is the substring or array we want to find and replace |
$replaceString |
Mandatory | 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 . |
$originalString |
force | It is the original string from which we want to find a substring or a characters to replace. |
##$count
| OptionalIt tells the total number of substitutions made to | $originalString Times
|
<?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; ?>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; ?>
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)The function preg_replace() accepts five parameters. Its detailed parameters are as follows
force |
It 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 | |
force |
It is a string or array from which we want to find the value and Replace it. | |
Optional |
This parameter limits the number of replacements | |
Optional |
This parameter tells us the total number of times to replace the original string or array |
to find spaces. The procedure to remove spaces from a string is as follows. <pre class="brush:php;toolbar:false"><?php
header("Content-type:text/html;charset=utf-8");
$originalString = "This is a programming tutorial";
$outputString = preg_replace(&#39;/\s+/&#39;, &#39;&#39;, $originalString);
echo "原字符串:".$originalString."<br>";
echo "去除空格后的字符串:".$outputString."<br>";
?></pre>
We 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('/\s+/', '', $originalString,$limit); echo "原字符串:".$originalString."<br>"; echo "去除空格后的字符串:".$outputString."<br>"; ?>
#It can be seen that it was only replaced twice.
Recommended learning: "
PHP Video TutorialThe 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!