473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I automatically add anchors to text links?

Hi,

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? http://warehouse.carlh.com/comic/comic_062.php

....but I want the html rendered to be this:

Hi! Great pics - have you seen this one? <a href="http://
warehouse.carlh.com/comic/comic_062.php">http://warehouse.carlh.com/
comic/comic_062.php</a>

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
article etc)

Thanks in advance.
Jun 2 '08 #1
3 1927
ha***********@gmail.com schreef:
Hi,

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? http://warehouse.carlh.com/comic/comic_062.php

...but I want the html rendered to be this:

Hi! Great pics - have you seen this one? <a href="http://
warehouse.carlh.com/comic/comic_062.php">http://warehouse.carlh.com/
comic/comic_062.php</a>

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
article etc)

Thanks in advance.
Hi,

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
display it.
In that case you also don't need to find the <div id="comments">
because, I expect, you add that when displaying the page.
Right?

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
href=""already.
Do you accept partial HTML in your postings? Like:
<span style="">some text</span>
or
<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).

Regards,
Erwin Moller
Jun 27 '08 #2
On 2 Jun, 14:05, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
harv3yb1rd...@gmail.com schreef:
Hi,
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?http://warehouse.carlh.com/comic/comic_062.php
...but I want the html rendered to be this:
Hi! Great pics - have you seen this one? <a href="http://
warehouse.carlh.com/comic/comic_062.php">http://warehouse.carlh.com/
comic/comic_062.php</a>
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
article etc)
Thanks in advance.

Hi,

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
display it.
In that case you also don't need to find the <div id="comments">
because, I expect, you add that when displaying the page.
Right?

So, for a certain fresh posting:
1) Find anything that starts with http://
2) Find the next space.
Of course this then falls down with normal sentence punctuation, e.g.:
Go and look at http://www.google.com.
Jun 27 '08 #3
Captain Paralytic:
Of course this then falls down with normal sentence punctuation, e.g.:
Good point!
Go and look at http://www.google.com.
Browsers should grok the FQDN in your example.

--
Jock
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ward | last post by:
Hi, I have a topframe and a mainframe. The topframe contains a listbox with trucknumbers (non sequential). The mainframe contains a page, where each truck has a table with data. In the...
2
by: mlv2312 | last post by:
Hi, I have experienced problems when dealing with nested anchors. I implemented some code to perform highlighting and specific anchors are used for the searched words. The problem is when the...
1
by: mlv2312 | last post by:
Hi, I have experienced problems when dealing with nested anchors. I implemented some code to perform highlighting and specific anchors are used for the searched words. The problem is when the...
2
by: learner | last post by:
Hi, A document has many Anchors. I want to take a particular action only if some particular anchors are clicked. I mean if some anchors are clicked, i want an alert box to pop up with ok and...
21
by: adrian suri | last post by:
Hi just started to experement with styleshhets, and have defined hover a:hover { Color : red; Text-decoration : none; Border-top-width : medium; Border-right-width : medium;
3
by: Mike Barnard | last post by:
Hi all, newbie here. Odd sounding subject but I can't describe it any better. I'm trying to teach myself a little about CSS. In a test site (not published) I am trying to use CSS to make...
1
by: eomer | last post by:
I have a lengthy page with several paragraphs. At the top of the page is a navigation bar with anchors to each of the paragraphs (the links). The problem is this: If I go to the page (after the...
4
by: Mario Vázquez | last post by:
Hi, I'm trying to put the links (href attribute of the <a> element) out of my XSLT files, in a XML file, and read it from the stylesheet. I suppose that I have to use the document() function to...
3
by: windandwaves | last post by:
does it matter if I write var anchors = document.getElementsByTagName("A"); or var anchors = document.getElementsByTagName("a"); Or is there a better way to catch both <a hrefs and <A...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.