首页 > php开发 > PHP清理字符串中所有无用标签

PHP清理字符串中所有无用标签

很多时候需要输出一些 "纯" 字符串,也就是去除任何杂质,例如 Html 标签、空格之类的文本,输出的摘要就是如此,下面的这个函数可以帮你实现着一点.

PHP实例代码如下:

function Bing_string_cleanr( $string ){ 
$string = trim( $string );  
$string = strip_tags( $string ); 
$string = htmlspecialchars( $string, ENT_QUOTES, 'UTF-8' ); 
$string = str_replace( "n", "", $string ); 
$string = trim( $string ); 
return $string; 
}

使用方法如下:

echo Bing_string_cleanr( '内 容 <br> <html> asdfeiuonsdfje' ); 

php删除空白,代码如下:

<?php 
$str = " This line containstliberal rn use of whitespace.nn"; 
 
// First remove the leading/trailing whitespace 
//去掉开始和结束的空白 
$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); 
//开源代码phprm.com 
// Echo out: 'This line contains liberal use of whitespace.' 
echo "<pre>{$str}</pre>";

   


本文地址:http://www.phprm.com/develop/fs5929.html

转载随意,但请附上文章地址:-)

标签:php清理标签 php清理字符串

发表留言