Home  >  Article  >  Backend Development  >  PHP regular expression to remove string hyperlink text_PHP tutorial

PHP regular expression to remove string hyperlink text_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:15759browse

Due to some very bad actions N made a long time ago, which resulted in some junk data on the website content page, this morning I developed a method to remove string hyperlink text. I will handle it with regular rules below.

The function of the following example is to filter all html tags and replace all text before h1-h5

The code is as follows Copy code
 代码如下 复制代码

for( $i=1;$i<=5;$i++ )
{

$sql ="SELECT * FROM `表名` WHERE `字段` like '%%' ";
 
 $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=[^>]*>|]*>/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;
}

for( $i=1;$i<=5;$i++ )
{

$sql ="SELECT * FROM `table name` WHERE `field` like '%%' ";

$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['field']);
$str = nl2br(strip_tags(addslashes(removelink($t))));
$sql="update table name set field='$str' where id=".$rs['id'];


If( mysql_query($sql))
{
echo $rs['id'].'Success
';
}
else
{
echo mysql_error();

}
}
else
{
echo 'It has been updated and there is no record'.$sql.'
';
}
 代码如下 复制代码

preg_replace("/(?is)(?<=

).*?(?=

)/i","",$t);

}
function removelink($t)
{
//$str = preg_replace("/]*href=[^>]*>|]*>/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;
}
The following regular expression is used above
The code is as follows Copy code
preg_replace("/(?is)(?<=

).*?(?=

)/i","",$t);

This is the core code


For example, if you need to remove hyperlink content from text, you need to use regular expressions. For example, you can use $str = preg_replace("/]*href=[^>]*>|]*>/i ","",$strhtml); This paragraph is used to implement the requirements. If you want more solutions, you can refer to the following.

1. Delete hyperlinks in content

The code is as follows
 代码如下 复制代码

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

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

Copy code


ereg_replace(']*)>([^<]*)','2 ',$content);
 代码如下 复制代码

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

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

2. Eliminate hyperlinks containing specific words

The code is as follows Copy code
$find="this string is my find"; $string='replaced';//Replace the hyperlink with content echo ereg_replace(']*)>([^<]*'.$find.'[^>]*)','2',$content); Original work on this site. Reprints must indicate the source www.bKjia.c0m, otherwise you will be responsible for the consequences
http://www.bkjia.com/PHPjc/633130.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633130.htmlTechArticleDue to some very bad actions done a long time ago, the website content page has some junk data. This morning, I have a method to remove string hyperlink text. Below I combine it with regular expressions...
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