473,805 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1949
ha***********@g mail.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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
harv3yb1rd...@g mail.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
1630
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 topleft cell of each table I put a named link <a name=$trucknumber>. Tables are generated using php. I want to use an onChange event in the listbox to jump to the corresponding
2
1626
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 searched words are inside <a href> tags, the links are lost after putting my anchors. For example: <a class="Programa" href="#" OnClick="window.open('/something.tif');"
1
2034
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 searched words are inside <a href> tags, the links are lost after putting my anchors. For example: <a class="Programa" href="#" OnClick="window.open('/something.tif');"
2
1888
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 cancel. and if ok is clicked i have to view that link, if cancel, then it should not open that link. How to do this?
21
2482
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
3393
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 navigation buttons. I'm using the tutorial I found at this site as a basis for it. http://www.mako4css.com/TFonts.htm
1
1628
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 cache and history have been cleared) the links are blue because I haven't clicked any of the links (this is expected). Now, if I click to another page and then return, all the links now indicate that they have been clicked. This is not what I...
4
1993
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 link to the XML file containing the URLs, but I don't know how to do it. I've tried something like this: This is XML file containing the URLs (link.xml): <?xml version="1.0" encoding="utf-8" ?> <LINKS>
3
16564
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 HREFS
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10613
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.