由于N久之前做了一些非常不好的动作导致网站内容页面有一些垃圾数据,今天早上整了一个移除字符串超链接文本方法,下面我结合正则来处理。
下面实例的功能是过滤所有的html标签,并替换h1-h5之前的所有文字
<?php for ($i = 1; $i <= 5; $i++) { $sql = "SELECT * FROM `表名` WHERE `字段` like '<h" . $i . ">%</h" . $i . ">%' "; $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['字段']); $str = nl2br(strip_tags(addslashes(removelink($t)))); $sql = "update 表名 set 字段='$str' where id=" . $rs['id']; if (mysql_query($sql)) { echo $rs['id'] . '成功<br />'; } else { echo mysql_error(); } } } else { echo '己更新过没有记录了' . $sql . '<br />'; } } 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('<h1>', '', $str); $str = str_replace('<h2>', '', $str); $str = str_replace('<h3>', '', $str); $str = str_replace('<h4>', '', $str); $str = str_replace('<h5>', '', $str); $str = str_replace('</h1>', '', $str); $str = str_replace('</h2>', '', $str); $str = str_replace('</h3>', '', $str); $str = str_replace('</h4>', '', $str); $str = str_replace('</h5>', '', $str); return $str; } ?>
上面用到了下面的正则表达式
preg_replace("/(?is)(?<=<h1 id="">).*?(?=</h1>)/i","",$t);
这就是核心代码了
比如需要将文本中的超链接内容去除,这个时候就需要用到正则表达式了。比如你可以用$str = preg_replace("/]*href=[^>]*>|[^a]*a[^>]*>/i","",$strhtml); 这段来实现需求,如果想要更多解决方法,可以参看以下的。
1、删除内容中的超链接
ereg_replace('<a([^>]*)>([^<]*)</a>','<font color="red">2</font>',$content); ereg_replace("<a [^>]*>|</a>","",$content);
2、消除包含特定词的超链接
$find="this string is my find";
$string='替换掉了';//将超链接替换成的内容
echo ereg_replace(']*)>([^]*)','2',$content);
本站原创,转载必须注明来源http://www.phprm.com 否则后果自负
永久地址:
转载随意~请带上教程地址吧^^

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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
Visual web development tools

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),
