If you want to remove whitespace from the beginning and end of the string, you can use the PHP internal function trim (). However, we often want to completely clear the blanks. You need to clear the opening and closing blanks, turn multiple whitespace into a blank, and use a rule to handle other whitespace of the same type.
Complete these can be done using a regular expression of PHP
The following example can remove additional whitespace
Copy the Code code as follows:
$STR = "This line contains\tliberal \ r \ n use of whitespace.\n\n";
First remove the leading/trailing whitespace
Remove the opening and closing blanks
$str = Trim ($STR);
Now remove any doubled-up whitespace
Remove the blank that follows the other squeeze
$str = Preg_replace ('/\s (? =\s)/', ' ', $str);
Finally, replace any non-space whitespace, with a space
Finally, remove the non-space space, with a space instead
$str = preg_replace ('/[\n\r\t]/', ' ', $str);
Echo out: ' This line contains liberal use of whitespace. '
echo "
{$STR}
";
?>
The previous example removes all the blanks step-by-step. First we use the trim () function to remove the opening and closing blanks. Then we use Preg_replace () to remove the duplicates. \s represents any whitespace. (? =) indicates a forward lookup. It tastes like a character that matches only the characters that follow the same character as itself. So the regular expression means: "Any whitespace character followed by the whitespace character." "We replace them with blanks, so we remove them, leaving the only whitespace characters."
Finally, we use another regular expression [\n\r\t] to find any remaining newline characters (\ n), carriage return (\ r), or tab (\ t). We replace these with a space.
The above describes the JS regular expression in PHP with regular expressions to clear the blank string, including the JS regular expression aspects of the content, I hope that the PHP tutorial interested in a friend helpful.