PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

PHP移除字符串超链接文本的正则表达式_PHP教程

原创
2016-07-13 10:44:15 559浏览

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

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

 代码如下 复制代码

for( $i=1;$i {

 $sql ="SELECT * FROM `表名` WHERE `字段` like '%".$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'].'成功
';
   }
   else
   {
    echo mysql_error();
   } 
  }
 }
 else
 {
  echo '己更新过没有记录了'.$sql.'
';
 }

}

 
function removelink($t)
{
 //$str = preg_replace("/]*href=[^>]*>|[^a]*a[^>]*>/i","",$t);
 
 $str = preg_replace("/(?is)(?).*?(?=)/i","",$t);
 $str = preg_replace("/(?is)(?).*?(?=)/i","",$str);
 $str = preg_replace("/(?is)(?).*?(?=)/i","",$str);
 $str = preg_replace("/(?is)(?).*?(?=)/i","",$str);
 $str = preg_replace("/(?is)(?).*?(?=)/i","",$str);

 return re_h($str);
}

function re_h($str)
{
 $str = str_replace('

','',$str);
 $str = str_replace('

','',$str);
 $str = str_replace('

','',$str);
 $str = str_replace('

','',$str);
 $str = str_replace('

','',$str);
 $str = str_replace('
','',$str);
 $str = str_replace('','',$str);
 $str = str_replace('','',$str);
 $str = str_replace('','',$str);
 $str = str_replace('','',$str); 
 return $str;
}

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

 代码如下 复制代码

preg_replace("/(?is)(?).*?(?=)/i","",$t);

这就是核心代码了


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

1、删除内容中的超链接

 代码如下 复制代码

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

ereg_replace("]*>|","",$content); 

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

 代码如下 复制代码

$find="this string is my find";
$string='替换掉了';//将超链接替换成的内容
echo ereg_replace(']*)>([^]*)','\2',$content);

本站原创,转载必须注明来源www.bKjia.c0m 否则后果自负

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633130.htmlTechArticle由于N久之前做了一些非常不好的动作导致网站内容页面有一些垃圾数据,今天早上整了一个移除字符串超链接文本方法,下面我结合正则来...
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。