由於N久之前做了一些非常不好的動作導致網站內容頁面有一些垃圾資料,今天早上整了一個移除字串超連結文本方法,下面我結合正則來處理。
下面執行個體的功能是過濾所有的html標籤,並替換h1-h5之前的所有文字
| 代碼如下 |
複製代碼 |
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; } |
上面用到了下面的Regex
| 代碼如下 |
複製代碼 |
preg_replace("/(?is)(?<= ).*?(?=)/i","",$t); |
這就是核心代碼了
比如需要將文本中的超連結內容去除,這個時候就需要用到Regex了。比如你可以用$str = preg_replace("/]*href=[^>]*>|]*>/i","",$strhtml); 這段來實現需求,如果想要更多解決方案,可以參看以下的。
1、刪除內容中的超連結
| 代碼如下 |
複製代碼 |
ereg_replace(']*)>([^<]*)','\2',$content); ereg_replace("]*>|","",$content); |
2、消除包含特定詞的超連結
| 代碼如下 |
複製代碼 |
$find="this string is my find"; $string='替換掉了';//將超連結替換成的內容 echo ereg_replace(']*)>([^<]*'.$find.'[^>]*)','\2',$content); |
本站原創,轉載必須註明來源www.bKjia.c0m 否則後果自負
http://www.bkjia.com/PHPjc/633130.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633130.htmlTechArticle由於N久之前做了一些非常不好的動作導致網站內容頁面有一些垃圾資料,今天早上整了一個移除字串超連結文本方法,下面我結合正則來...