If you want to remove whitespace at the beginning and end of a string you can use the PHP internal function trim(). However, we often want to clear the white space completely. You need to clear the starting and ending blanks, turn multiple blanks into one blank, and use a rule to handle other blanks of the same type.
This can be done using PHP regular expressions
The following example can remove extra Whitespace
Copy the code The code is as follows:
$str = " This line containstliberal rn use of whitespace. nn";
// First remove the leading/trailing whitespace
//Remove the starting and ending whitespace
$str = trim($str);
// Now remove any doubled-up whitespace
//Remove following other squeezes In a piece of whitespace
$str = preg_replace('/s(?=s)/', '', $str);
// Finally, replace any non-space whitespace, with a space
//Finally, remove the non-space space, replace it with a space
$str = preg_replace('/[nrt]/', ' ', $str);
// Echo out: 'This line contains liberal use of whitespace.'
echo "< pre>{$str}";
?>
The above introduces the use of regular expressions in PHP to clear the blanks of strings in js regular expressions, including the content of js regular expressions. I hope it will be helpful to friends who are interested in PHP tutorials.