Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
June 2, 2008, 12:17 am
rate this thread
I have a blog which people often include a link in their comment.
Can anyone point me to a script or demonstrate how I can automatically
surround the text links with an anchor when the page is rendered? As
an example, here is a typical comment:
Hi! Great pics - have you seen this one?
...but I want the html rendered to be this:
Hi! Great pics - have you seen this one? <a href="http://
I should also point out I only want this to happen in the comments
area, which is contained in a div called 'comments'. Either that, or
the script will need to ignore existing anchors (like in the original
Thanks in advance.
Re: How can I automatically add anchors to text links?
Well, you described yourself what you need.
For starters: I think it the easiest to process a fresh posting so it
contains the desired HTML instead of parsing the content every time you
In that case you also don't need to find the <div id="comments">
because, I expect, you add that when displaying the page.
So, for a certain fresh posting:
1) Find anything that starts with http://
2) Find the next space.
The piece of text inbetween can be a valid hyperlink.
Of course, it can also be nonsense.
Do you want to check this too?
Next step is, possibly, to check if the poster did provide the <a
Do you accept partial HTML in your postings? Like:
<span style="">some text</span>
<a href="whatever">my link</a>
This can quickly become complex, but since you didn't provide the
details on WHAT is posted, it is hard to help you with good advise.
So, bottomline, do you accept html in your postings?
If not, I think you can easily add the hyperlinks.
This can be done with standard stringfunctions, or with a regex.
If you do, and accept partial html, we enter a swamp (I expect).