Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

conveting link text to link! in php

  • 17-05-2007 07:27PM
    #1
    Registered Users, Registered Users 2 Posts: 648 ✭✭✭


    hi

    anyone know how to :

    if i have a link in a passage of text how i can actually change that to become clickable ? (using a regular expressions or something)

    (would be nice if i could limit the text displayed between the <a></a> tags just in case of loooooooong urls

    Tnx


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Can you clarify?

    Do you have a passage of plaintext, or a passage of HTML-formatted text?

    Are you looking for something similar to what's here on boards, i.e. when I type in www.someaddress.com, it automatically adds in the link tags?


  • Registered Users, Registered Users 2 Posts: 648 ✭✭✭ChicoMendez


    seamus wrote:
    Can you clarify?

    Do you have a passage of plaintext, or a passage of HTML-formatted text?

    Are you looking for something similar to what's here on boards, i.e. when I type in www.someaddress.com, it automatically adds in the link tags?

    hi

    yes its plain text - and yes exactly as you said (like its done here)

    tnx


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Regular expressions will be your friend then.

    You should probably try to identify three types of URLS;

    Fully-qualified web addresses, e.g. http://host.boards.ie/ or http://host.boards.ie

    Web addresses without a protocal (they can be assumed to be an HTTP url if they start with "www.", e.g. www.boards.ie or www.google.com

    Web addresses (fully-qualified or otherwise) with a pointer to a specific document, e.g. www.boards.ie/somepage.php or http://www.google.com/somepage.php

    In general, I would isolate web addresses by finding something that begins with "http://&quot; or "www." and selecting all text I can find until I locate a character which you wouldn't find in a URL.
    You could easily say that the character set which you would find in a URL consists of [A-Za-z0-9?/\:&%.]. Though someone could easily point out any I've missed.

    Then you take that matched text, replace it with <a href="(http://)$matched_text">$matched_text</a&gt;

    I say (http://) because you will need to add that in if the text just starts with www.


  • Registered Users, Registered Users 2 Posts: 648 ✭✭✭ChicoMendez


    seamus wrote:
    Regular expressions will be your friend then.

    You should probably try to identify three types of URLS;

    Fully-qualified web addresses, e.g. http://host.boards.ie/ or http://host.boards.ie

    Web addresses without a protocal (they can be assumed to be an HTTP url if they start with "www.", e.g. www.boards.ie or www.google.com

    Web addresses (fully-qualified or otherwise) with a pointer to a specific document, e.g. www.boards.ie/somepage.php or http://www.google.com/somepage.php

    In general, I would isolate web addresses by finding something that begins with "http://&quot; or "www." and selecting all text I can find until I locate a character which you wouldn't find in a URL.
    You could easily say that the character set which you would find in a URL consists of [A-Za-z0-9?/\:&%.]. Though someone could easily point out any I've missed.

    Then you take that matched text, replace it with <a href="(http://)$matched_text">$matched_text</a&gt;

    I say (http://) because you will need to add that in if the text just starts with www.

    GREAT
    ok i have this so far


    <?
    $str='http://www.com.com kasjh kjashk dahskjhsakjhsk hskahsaskjhskjj www.com.com';
    $str = ereg_replace(":alpha:+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $str);


    echo $str;
    ?>

    which transforms the http: links
    however how to i regular expression the www. links (no http) so as they dont replace the links ive just changed !

    tnx


Advertisement