One, the following example can remove extra whitespace
The code is as follows |
|
<?php $STR = "This line containstliberal RN with Whitespace.nn"; The leading/trailing whitespace Remove the start and end blank Www.111cn.net $str = Trim ($STR); Now remove any doubled-up whitespace To take the rest of the space and squeeze it in. $str = Preg_replace ('/s (? =s)/', ', $str); Finally, replace any non-space whitespace and a space Finally, remove the blank space, using a space instead $str = preg_replace ('/[nrt]/', ', ', $str); Echo out: ' This line contains liberal the use of whitespace. ' echo "<pre>{$str}</pre>"; ?> |
Second, replace line feed
PHP has three ways to solve
The code is as follows |
|
1. Use Str_replace to replace line wrapping $str = Str_replace (Array ("RN", "R", "N"), "", $str); 2, the use of regular replacement $str = preg_replace ('//s*/', ', ', $str); 3. Use PHP to define a good variable (recommended) $str = Str_replace (Php_eol, ', $str); |
The code is as follows:
The code is as follows |
|
/* * Get line breaks for 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 '; the right to a significant point www.111cn.net } return $the _crlf; } |
Note: When the front page is displayed, use the NL2BR to change the line into
Third, replace carriage return
code is as follows |
&nbs P; |
<?php &nbs p; //php the line-wrapping of different systems; //The implementation of line wrapping between different systems is not the same / linux with Unix/n //mac with/r //window In order to be different from Linux is the/r/n //So the implementation of the method on different platforms is not the same //php There are three ways to solve /1, Use Str_replace to replace newline $str = str_replace (Array ("/r/n", "/R", "/n"), "", $str); & NBSP;&NBSP //2, using regular substitution &NBSP;&NBSP $str = preg_replace ('//s*/', ', $str); 3. Use PHP-defined variables (recommended) $str = str_replace (Php_eol, ', $str); > |