How to make link attribute to nofollow in all links

How to make link attribute to nofollow in all links?

I have some lines of text, which also contains links and images. But i want to know any way so that without changing the text from inside, I could make all links with rel=nofollow.

using either preg_replace or something else.

But I don’t know the way to start with.
Can someone guide me?

You have two options there.

  1. Use regular expressions to process the HTML code
  2. Convert the text to an XML structure, then process from there

In this case, I might be tempted to go with the second option.