search
Homephp教程php手册PHP移除字符串超链接文本的正则表达式

由于N久之前做了一些非常不好的动作导致网站内容页面有一些垃圾数据,今天早上整了一个移除字符串超链接文本方法,下面我结合正则来处理。

下面实例的功能是过滤所有的html标签,并替换h1-h5之前的所有文字

<?php
for ($i = 1; $i <= 5; $i++) {
    $sql = "SELECT * FROM `表名` WHERE `字段` like &#39;<h" . $i . ">%</h" . $i . ">%&#39; ";
    $query = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($query)) {
        while ($rs = mysql_fetch_array($query)) {
            //print_r($rs);
            $t = stripslashes($rs[&#39;字段&#39;]);
            $str = nl2br(strip_tags(addslashes(removelink($t))));
            $sql = "update 表名 set 字段=&#39;$str&#39; where id=" . $rs[&#39;id&#39;];
            if (mysql_query($sql)) {
                echo $rs[&#39;id&#39;] . &#39;成功<br />&#39;;
            } else {
                echo mysql_error();
            }
        }
    } else {
        echo &#39;己更新过没有记录了&#39; . $sql . &#39;<br />&#39;;
    }
}
function removelink($t) {
    //$str = preg_replace("/<a[^>]*href=[^>]*>|</[^a]*a[^>]*>/i","",$t);
    $str = preg_replace("/(?is)(?<=<h1 id="">).*?(?=</h1>)/i", "", $t);
    $str = preg_replace("/(?is)(?<=<h2 id="">).*?(?=</h2>)/i", "", $str);
    $str = preg_replace("/(?is)(?<=<h3 id="">).*?(?=</h3>)/i", "", $str);
    $str = preg_replace("/(?is)(?<=<h4 id="">).*?(?=</h4>)/i", "", $str);
    $str = preg_replace("/(?is)(?<=<h5 id="">).*?(?=</h5>)/i", "", $str);
    return re_h($str);
}
function re_h($str) {
    $str = str_replace(&#39;<h1>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;<h2>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;<h3>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;<h4>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;<h5>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;</h1>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;</h2>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;</h3>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;</h4>&#39;, &#39;&#39;, $str);
    $str = str_replace(&#39;</h5>&#39;, &#39;&#39;, $str);
    return $str;
}
?>

上面用到了下面的正则表达式

preg_replace("/(?is)(?<=<h1 id="">).*?(?=</h1>)/i","",$t);

这就是核心代码了


比如需要将文本中的超链接内容去除,这个时候就需要用到正则表达式了。比如你可以用$str = preg_replace("/]*href=[^>]*>|[^a]*a[^>]*>/i","",$strhtml); 这段来实现需求,如果想要更多解决方法,可以参看以下的。

1、删除内容中的超链接

ereg_replace(&#39;<a([^>]*)>([^<]*)</a>&#39;,&#39;<font color="red">2</font>&#39;,$content);
ereg_replace("<a [^>]*>|</a>","",$content);

2、消除包含特定词的超链接

$find="this string is my find";

$string='替换掉了';//将超链接替换成的内容

echo ereg_replace(']*)>([^]*)','2',$content);

本站原创,转载必须注明来源http://www.phprm.com 否则后果自负


永久地址:

转载随意~请带上教程地址吧^^

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),