찾다
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 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기