2017-05-22 27 views
-3

我不知道如何實現以下內容:選擇從DATABSE <a>標籤,並添加相對=「nofollow」

我有一個數據庫表「信息」包含ID,內容,我有超過20000個帖子在帖子表,我想讓mysql查詢找到內容記錄中沒有rel =「nofollow」屬性的所有外部標籤,然後添加rel =「nofollow」並更新帖子。

關於如何實現這個的任何建議?

+0

您需要先嚐試一下。向我們展示您嘗試過的代碼,然後我們會從那裏幫助您。 – CodeGodie

+0

我會給你提示,使用存儲過程使其更容易,並看看這個:https://dev.mysql.com/doc/refman/5.7/en/string-functions.html – divinemaniac

+2

考慮_not_修改數據,而是'固定'輸出_after_提取數據並顯示之前。 –

回答

0

我在Stackoverflow找到了這個解決方案,找不到對不起的參考。它爲我工作。

$content = nofollow($content,'example.com'); 
function nofollow($html, $skip = null) { 
    return preg_replace_callback(
     "#(<a[^>]+?)>#is", function ($mach) use ($skip) { 
      return (

       !($skip && strpos($mach[1], $skip) !== false) && 
       strpos($mach[1], 'rel=') === false 
      ) ? $mach[1] . ' rel="nofollow">' : $mach[0]; 
     }, 
     $html 
    ); 
}