One, the following example can remove additional whitespace
| The code is as follows |
|
|
$STR = "This line containstliberal RN use of whitespace.nn";
First remove the leading/trailing whitespace
Remove the opening and closing blanks www.111cn.net
$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 ('/[nrt]/', ' ', $str);
Echo out: ' This line contains liberal use of whitespace. '
echo "
{$STR}";
?>
|
Second, replace the line break
PHP has three ways to solve
| The code is as follows |
|
|
1. Use Str_replace to replace line breaks
$str = Str_replace (Array ("RN", "R", "N"), "", $str);
2. Use regular replacement
$str = preg_replace ('//s*/', ' ', $str);
3. Use PHP-defined variables (recommended)
$str = Str_replace (Php_eol, ", $STR); |
The code is as follows:
| The code is as follows |
|
/*
* Get the user's operating system line break, n
* @access Public
* @return String
*/
function Get_crlf ()
{
if (Stristr ($_server[' http_user_agent '), ' Win ')
{
$the _crlf = ' RN ';
}
ElseIf (Stristr ($_server[' http_user_agent '), ' Mac ')
{
$the _crlf = ' R '; For-old MAC OS
}
Else
{
$the _crlf = ' n ';//Right important point www.111cn.net
}
return $the _crlf;
} |
Note: When the foreground page is displayed, use NL2BR to change the line to
Third, replace the carriage return
| The code is as follows |
|
|
PHP line breaks for different systems
The implementation of line breaks between different systems is not the same
Linux and Unix with/n
MAC with/R
Window is/r/n in order to show that it is different from Linux
So the implementation method on different platforms is not the same
PHP has three ways to solve
1. Use Str_replace to replace line breaks
$str = Str_replace (Array ("/r/n", "/R", "/n"), "", $str);
2. Use regular replacement
$str = preg_replace ('//s*/', ' ', $str);
3. Use PHP-defined variables (recommended)
$str = Str_replace (Php_eol, ", $STR);
?> |
http://www.bkjia.com/PHPjc/727587.html www.bkjia.com true http://www.bkjia.com/PHPjc/727587.html techarticle One, the following example can remove the additional blank code as follows? PHP $str = This line containstliberal rn use of whitespace.nn;//First remove the leading/trailing whitespace//Remove start and end ...