搜尋
首頁php教程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 否则后果自负


永久地址:

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),