user3197354 user3197354 - 6 months ago 14
PHP Question

Extracting link from a href and putting it within brackets

I have searched and couldn't find what I was looking for.

This is how it will normally be:

<p>
Hi how are you? Have you checked <a href="http://www.google.com">Google</a> or <a href="http://www.youtube.com">YouTube</a>?
</p>


But what I would love to have, because I'm willing to apply this on a print or PDF page is the following:

<p>
Hi how are you? Have you checked <a href="http://www.google.com">Google</a> (<a href="http://www.google.com">http://www.google.com</a>)
or <a href="http://www.youtube.com">YouTube</a> (<a href="http://www.youtube.com">http://www.youtube.com</a>)?
</p>


Now, I understand that there should be some work with regex, but I don't know how to use that. The text will be taken from the variable $content which has the article in it, and what I would like to have is that all links within $content remain as they are plus the content of href be as an additional hyperlink within brackets "()" so that, hypothetically when someone reads a printed article where hyperlinks are they would be able to see the actual URL.

Answer

Use a pseudo element to add the href after your links, something like:

a[href]:after  
{
    content: " (" attr(href) ") ";
}