Use the php regular expression to filter the blank line breaks and press enter. 1. the following example shows how to remove extra blank code? Php $ strThislinecontainstliberalrnuseofwhitespace. nn; Firstremovetheleadingtrailingwhitespace remove start and end 1. the following example can remove extra spaces.
The code is as follows: |
|
$ 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" {$str} "; ?> |
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: |
|
// 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 ); ?> |
The pipeline code is as follows? Php $ str = This line containstliberal rn use of whitespace. nn; // First remove the leading/trailingwhitespace // remove start and end...