Search code examples
phpregexyiiyii2htmlpurifier

Remove attribute rel follow from Tag a and replace value nofollow


I would like to remove attribute rel following a tag a and replace the value with nofollow using PHP or Yii2, Regex, or HtmlPurifier.

I use Yii2.

Content:

<a href="http://www.hgjhg.com" rel="follow">
<a href="http://www.gggg.com">
<a href="http://www.kjhjg.com/511" rel="nofollow">

I would like:

<a href="http://www.hgjhg.com" rel="nofollow">
<a href="http://www.gggg.com" rel="nofollow">
<a href="http://www.kjhjg.com/511" rel="nofollow">

Solution

  • add nofollow to tag a

    public static function addNoFollow($html)
    {
         $result = HtmlPurifier::process($html, function ($config) {
         $config->set('HTML.Nofollow', true);
         });
    
         return $result;
    }