How can I automatically add anchors to text links?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • harv3yb1rdman@gmail.com

    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.
  • Erwin Moller

    #2
    Re: How can I automatically add anchors to text links?

    harv3yb1rdman@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

    Comment

    • Captain Paralytic

      #3
      Re: How can I automatically add anchors to text links?

      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.

      Comment

      • John Dunlop

        #4
        Re: How can I automatically add anchors to text links?

        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

        Comment

        Working...