PHP regular expression to filter spaces, newlines, and carriage returns_PHP Tutorial

WBOY
Release: 2016-07-13 10:39:58
Original
1326 people have browsed it

   一,下例可以去除额外空白

 代码如下  

$str = " This line  containstliberal rn use of   whitespace.nn"; 
// First remove the leading/trailing whitespace 
//去掉开始和结束的空白  www.111cn.net
$str = trim($str); 
// Now remove any doubled-up whitespace 
//去掉跟随别的挤在一块的空白 
$str = preg_replace('/s(?=s)/', '', $str); 
// Finally, replace any non-space whitespace, with a space 
//最后,去掉非space 的空白,用一个空格代替 
$str = preg_replace('/[nrt]/', ' ', $str); 
// Echo out: 'This line contains liberal use of whitespace.' 
echo "

{$str}
"; 
?>

 代码如下  

$str = " This line  containstliberal rn use of   whitespace.nn";
// First remove the leading/trailing whitespace
//去掉开始和结束的空白 www.111cn.net
$str = trim($str);
// Now remove any doubled-up whitespace
//去掉跟随别的挤在一块的空白
$str = preg_replace('/s(?=s)/', '', $str);
// Finally, replace any non-space whitespace, with a space
//最后,去掉非space 的空白,用一个空格代替
$str = preg_replace('/[nrt]/', ' ', $str);
// Echo out: 'This line contains liberal use of whitespace.'
echo "

{$str}
"; 
?>

  二,替换换行符

  //php 有三种方法来解决

 代码如下  

//1、使用str_replace 来替换换行 
$str = str_replace(array("rn", "r", "n"), "", $str);

 代码如下  

//1、使用str_replace 来替换换行 
$str = str_replace(array("rn", "r", "n"), "", $str);

//2、使用正则替换 
$str = preg_replace('//s*/', '', $str);

//3、使用php定义好的变量 (建议使用) 
$str = str_replace(PHP_EOL, '', $str);

//2、使用正则替换 
$str = preg_replace('//s*/', '', $str);

//3、使用php定义好的变量 (建议使用) 
$str = str_replace(PHP_EOL, '', $str);

The code is as follows:

 代码如下  
 
/* 
* 获得用户操作系统的换行符,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

return $the_crlf; 
}

Note: When displaying on the front page, use nl2br to change the line break to

Three, replace the carriage return

The code is as follows

//php line breaks in different systems
//The implementation of line breaks is different between different systems
//Used in linux and unix /n
//For MAC /r
//window In order to reflect the difference from linux, it is /r/n
//So the implementation methods are different on different platforms
//php has three ways to solve it
                                                                                //1. Use str_replace to replace newlines
$str = str_replace(array("/r/n", "/r", "/n"), "", $str);
                                                                                //2. Use regular replacement
$str = preg_replace('//s*/', '', $str);
                                                                                //3. Use variables defined by php (recommended)
$str = str_replace(PHP_EOL, '', $str);
?>

 代码如下  

    //php 不同系统的换行
//不同系统之间换行的实现是不一样的
//linux 与unix中用 /n
//MAC 用 /r
//window 为了体现与linux不同 则是 /r/n
//所以在不同平台上 实现方法就不一样
//php 有三种方法来解决

//1、使用str_replace 来替换换行
$str = str_replace(array("/r/n", "/r", "/n"), "", $str);

//2、使用正则替换
$str = preg_replace('//s*/', '', $str);

//3、使用php定义好的变量 (建议使用)
$str = str_replace(PHP_EOL, '', $str);
?> 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/727587.htmlTechArticleFirst, the following example can remove extra whitespace code as follows?php $str = This line containstliberal rn use of whitespace.nn ; // First remove the leading/trailingwhitespace //Remove the beginning and end...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template