Home > php教程 > php手册 > body text

PHP删除HTMl标签的实现代码

WBOY
Release: 2016-06-13 11:43:30
Original
699 people have browsed it

复制代码 代码如下:


/**
 * 取出html标签
 *
 * @access public
 * @param string str
 * @return string
 *
 */
function deletehtml($str) {
    $str = trim($str); //清除字符串两边的空格
    $str = strip_tags($str,"

"); //利用php自带的函数清除html格式。保留P标签
    $str = preg_replace("/\t/","",$str); //使用正则表达式匹配需要替换的内容,如:空格,换行,并将替换为空。
    $str = preg_replace("/\r\n/","",$str);
    $str = preg_replace("/\r/","",$str);
    $str = preg_replace("/\n/","",$str);
    $str = preg_replace("/ /","",$str);
    $str = preg_replace("/  /","",$str);  //匹配html中的空格
    return trim($str); //返回字符串
}

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!