PHP Regular expression filter Space line newline character carriage return

Source: Internet
Author: User
Tags php regular expression regular expression

One, the following example can remove extra whitespace

The code is as follows

$STR = "This line containstliberal RN with Whitespace.nn";
The leading/trailing whitespace
Remove the start and end blank
$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
$the _crlf = ' n '; the right to a significant point
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;

  &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);   

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.