Home >php教程 >php手册 >PHP 清理字符串中所有无用标签

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

WBOY
WBOYOriginal
2016-05-25 16:46:341094browse

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

<?php
function Bing_string_cleanr($string) {
    $string = trim($string);
    $string = strip_tags($string);
    $string = htmlspecialchars($string, ENT_QUOTES, &#39;UTF-8&#39;);
    $string = str_replace("n", "", $string);
    $string = trim($string);
    return $string;
}
?>

使用方法:

echo Bing_string_cleanr( &#39;内 容 <br> <html> asdfeiuonsdfje&#39; );

删除空白

<?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(&#39;/s(?=s)/&#39;, &#39;&#39;, $str);
// Finally, replace any non-space whitespace, with a space
//最后,去掉非space 的空白,用一个空格代替
$str = preg_replace(&#39;/[nrt]/&#39;, &#39; &#39;, $str);
// Echo out: &#39;This line contains liberal use of whitespace.&#39;
echo "<pre class="brush:php;toolbar:false">{$str}
"; ?>


永久链接:

转载随意!带上文章地址吧。

Statement:
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