How to remove spaces from lines in PHP?

When working with text, you often have to format it. This is necessary for correct display and easy readability. This is necessary if the user enters some information and makes mistakes: instead of one space, indicates two, at the beginning puts a tab. There are several ways to remove spaces in PHP.

Trim ()

The Trim function searches for excess characters at the beginning of a line or at the end. It:

  • regular space;
  • tabulation
  • line break character.

It is written in this form:

string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) 

$ str is the string to be processed, and $ character_mask is the extra characters. $ character_mask is an optional attribute.

Preg_replace

Function for searching and replacing characters by regular expression.

Trim () function in PCP
 mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 
  1. $ pattern - the desired pattern.
  2. $ replacement - characters to replace.
  3. $ subject - the processed object.
  4. $ limit - the number of replacements made.

$ pattern and $ replacement can be arrays. In this case, the replacement is made in accordance with the indices.

Str_replace ()

You can remove spaces from a string in PHP using the str_replace () method. It replaces all occurrences of the search string with a replacement string.

 mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) 

Used as a simplified preg_replace () method.

  1. $ search is the value to find.
  2. $ replace is the string to replace.
  3. $ subject - the object in which the search and replacement is performed.
  4. $ count sets the number of replacements.
 <?php //  <body text='black'> $bodytag = str_replace("%body%", "black", "<body text='%body%'>"); // : Hll Wrld f PHP $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); // : You should eat pizza, beer, and ice cream every day $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); // : 2 $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; ?> 

Search and replace spaces example

These functions are used in most situations, even in more complex ones.

YP PHP

For example, a user has entered some data that will later be printed on the screen. To improve the readability and perception of the text as a whole, this information needs to be processed - remove repeated spaces, replace them with single ones.

 $text1 = "     "; 

In this case, it can be seen that there are two and three spaces between the words. The procedure for removing spaces in PHP is as follows.

1. First you need to turn a string into an array of strings using a function.

 explode(β€œ ”, $text1)​ 

A single space is used as a separator. Thus, body parts that are not separate elements of the array will contain one space less.

2. The result is an array of strings:

 $array = [" β€œ, β€œβ€, β€œβ€, β€œ β€œ, β€œ "]​ 

3. Each element is processed by the function:

 preg_replace('/\s+/', ' ', $text1)​ 

To search for one or more spaces, use the regular expression / \ s + /. All matches found are replaced by the string. '' Search is carried out in the variable $ text1.

4. As a result, we get a string with the correct number of spaces, which is easily perceived by the user.

Source: https://habr.com/ru/post/K3473/


All Articles