1. The following example can remove extra blank space.
The code is as follows: |
Copy code |
<? 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: |
Copy code |
// 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: |
Copy code |
/* * 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 to <br>
3. Replace the carriage return
The code is as follows: |
Copy code |
<? 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 ); ?> |
The Yunqi community editor reminds you that sometimes we also need to pay attention to the need for chr (32), chr (13), and line breaks like/r/n in textarea) this can be replaced.