Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

conveting link text to link! in php

  • 17-05-2007 6: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,317 ✭✭✭✭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,317 ✭✭✭✭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