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

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

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.