Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4583240
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:11:14+00:00 2026-05-21T21:11:14+00:00

I have my application to allow users to write comments on my website. Its

  • 0

I have my application to allow users to write comments on my website. Its working fine. I also have tool to insert their weblinks in it. I feel good with contents with their own weblinks.

Now i want to add rel=”nofollow” to every links on content that they have been written.

I would like to add rel=”nofollow” using php i.e while saving data.

So what’s a simple method to add rel=”nofollow” or updated rel=”someother” with rel=”someother nofollow” using php

a nice example will be much efficient

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:11:15+00:00Added an answer on May 21, 2026 at 9:11 pm

    Regexs really aren’t the best tool for dealing with HTML, especially when PHP has a pretty good HTML parser built in.

    This code will handle adding nofollow if the rel attribute is already populated.

    $dom = new DOMDocument;
    
    $dom->loadHTML($str);
    
    $anchors = $dom->getElementsByTagName('a');
    
    foreach($anchors as $anchor) { 
        $rel = array(); 
    
        if ($anchor->hasAttribute('rel') AND ($relAtt = $anchor->getAttribute('rel')) !== '') {
           $rel = preg_split('/\s+/', trim($relAtt));
        }
    
        if (in_array('nofollow', $rel)) {
          continue;
        }
    
        $rel[] = 'nofollow';
        $anchor->setAttribute('rel', implode(' ', $rel));
    }
    
    var_dump($dom->saveHTML());
    

    CodePad.

    The resulting HTML is in $dom->saveHTML(). Except it will wrap it with html, body elements, etc, so use this to extract just the HTML you entered…

    $html = '';
    
    foreach($dom->getElementsByTagName('body')->item(0)->childNodes as $element) {
        $html .= $dom->saveXML($element, LIBXML_NOEMPTYTAG);
    }
    
    echo $html;
    

    If you have >= PHP 5.3, replace saveXML() with saveHTML() and drop the second argument.

    Example

    This HTML…

    <a href="">hello</a>
    
    <a href="" rel="">hello</a>
    
    <a href="" rel="hello there">hello</a>
    
    <a href="" rel="nofollow">hello</a>
    

    …is converted into…

    <a href="" rel="nofollow">hello</a>
    
    <a href="" rel="nofollow">hello</a>
    
    <a href="" rel="hello there nofollow">hello</a>
    
    <a href="" rel="nofollow">hello</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that allows users to write their own code in a
I have an application that needs to allow users to write expressions similar to
We have a high security application and we want to allow users to enter
We have created a web application, using ASP.NET, that allows users to upload documents
I have an application which is a portal application and I want to allow
I have an application which really should be installed, but does work fine when
I have been asked to write a custom webpage in a web application integrated
I'm using a PropertyGrid in an application I am writing to allow users to
I have a web application that allows a user to search on some criteria,
I have a bowling web application that allows pretty detailed frame-by-frame information entry. One

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.