Home > Article > Backend Development > PHP regular expression to remove string hyperlink text_PHP tutorial
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 | ||||||||
{ $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.' '; }
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 = str_replace(' ','',$str);$str = str_replace(' ','',$str); |
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=[^>]*>|[^a]*a[^>]*>/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
|
Copy code
|
||||
ereg_replace(']*)>([^<]*)','2 ',$content);
ereg_replace("]*>|","",$content); |
2. Eliminate hyperlinks containing specific words