Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php removes all spaces in a string

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to remove all spaces in a string by php". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "php how to remove all spaces in a string"!

Php methods to remove all spaces in the string: 1, use the str_replace () function, the syntax "str_replace (", ", $str)"; 2, with the preg_replace () function, the syntax "preg_replace ('/\ s magic pedigree)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of removing all spaces in a string by php

Use the str_replace () function to remove all spaces

Use the preg_replace () function to remove all spaces

1. Use the str_replace () function

We use the built-in function str_replace () to replace substrings in a string or array. 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 () takes four parameters. Its detailed parameters are as follows

Parameters.

Note that $searchString forces it to be a substring or array of $replaceString that we want to find and replace. Force it to be a string where we want to put $searchString. The function checks for $searchString and replaces it with $replaceString. This function checks for the occurrence of $searchString and replaces it with $replaceString. It can also be an array. OriginalString forces it to be the original string, and we want to find a substring or a character to replace it. $count is optional. It tells people the total number of times to replace $originalString.

This function returns the final string of all substitutions.

The following program shows how we can use the str_replace () function to remove all spaces from a given string.

We pass a space character as $searchString and an empty string as $replaceString. The output will be a string with no spaces.

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

2. Use the preg_replace () function

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

Preg_replace ($regexPattern, $replacementVar, $original, $limit, $count)

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

Parameters.

Note that $regexPattern forces it to be the pattern we will search in the original string or array $replacementVar forces it to be the string or array we use to replace the search value $original forces it to be a string or array from which we find the value and replace it. Optional $limit this parameter limits the number of replacements $count optional this parameter tells us the total number of times to replace the original string or array

We will use the pattern /\ sspaces / to find spaces. The procedure for removing spaces from a string is as follows.

We know that the total number of substitutions for this string is 4, and now we will limit the number of substitutions.

You can see that it has only been replaced twice.

At this point, I believe you have a deeper understanding of "php how to remove all spaces in the string". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report