There are spaces and ascill spaces in php, so we need to sort them together to replace them, let's take a look at an example of php replacement and filtering of all the blank characters and spaces. In php, there are spaces and ascill spaces. So we need to sort the replacement items together, the following is an example of replacing and filtering all blank characters and spaces with php.
Script ec (2); script
The self-contained trim function in php can only replace spaces on both sides of the left and right. In some cases, it may not be easy to use, if you want to filter out all the spaces (spaces, full-width spaces, line breaks, etc.) in a string, you can write a filter function by yourself.
Php learns str_replace functions and can be replaced in batches. Therefore, we can use the following source code to replace and filter all the blank characters in a string.
Php source code reference:
$ Str = 'jkgsd
Gsgsdgs gsdg gsd ';
Echo myTrim ($ str );
Function myTrim ($ str)
{
$ Search = array ("", "", "\ n", "\ r", "\ t ");
$ Replace = array ("","","","","");
Return str_replace ($ search, $ replace, $ str );
}
?>
Run the code and the page output: jkgsdgsgsdgsgsdggsd, which perfectly achieves the desired effect.
To do this, you can use the PHP regular expression.
In the following example, additional Whitespace can be removed.
$ Str = "This line contains \ tliberal \ r \ n use of whitespace. \ n ";
// First remove the leading/trailing whitespace
// Remove the start and end spaces.
$ Str = trim ($ str );
// Now remove any doubled-up whitespace
// Remove the blank space crowded with other items
$ Str = preg_replace ('/\ s (? = \ S)/', '', $ str );
// Finally, replace any non-space whitespace, with a space
// Finally, remove the non-space blank and replace it with a space.
$ Str = preg_replace ('/[\ n \ r \ t]/', '', $ str );
// Echo out: 'This line contains liberal use of whitespace .'
Echo"
{$str}
";
?>
In this example, extra white space characters are removed.
$ Str = 'foo o ';
$ Str = preg_replace ('/\ s +/', '', $ str );
// It will be changed to 'foo o'
Echo $ str;
?>