1. The following example can remove Extra blank space.
| The Code is as follows: |
|
|
<? Php
$ Str = "This line containstliberal rn use of whitespace. nn ";
// First remove the leading/trailing whitespace
// Remove the start and end blank www.111cn.net
$ 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 ('/[nrt]/', '', $ str );
// Echo out: 'This line contains liberal use of whitespace .'
Echo "<pre >{$ str} </pre> ";
?> |
2. Replace line breaks
// Php has three solutions
| The Code is as follows: |
|
|
// 1. Use str_replace to replace the line feed.
$ Str = str_replace (array ("rn", "r", "n"), "", $ str );
// 2. Use Regular Expression replacement
$ Str = preg_replace ('// s */', '', $ str );
// 3. Use the variables defined in php (recommended)
$ Str = str_replace (PHP_EOL, '', $ str ); |
The Code is as follows:
| The Code is as follows: |
|
/*
* Get the line break of the user's operating system, 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'; // www.111cn.net with a higher weight
}
Return $ the_crlf;
} |
Note: Use nl2br to change the line feed
3. Replace the carriage return
| The Code is as follows: |
|
|
<? Php
// Line feed for different php Systems
// The implementation of line feed varies with systems.
// In linux and unix/n
// For MAC/r
// Window is/r/n to reflect differences from linux
// The implementation methods on different platforms are different.
// Php has three solutions
// 1. Use str_replace to replace the line feed.
$ Str = str_replace (array ("/r/n", "/r", "/n"), "", $ str );
// 2. Use Regular Expression replacement
$ Str = preg_replace ('// s */', '', $ str );
// 3. Use the variables defined in php (recommended)
$ Str = str_replace (PHP_EOL, '', $ str );
?> |