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

conveting link text to link! in php

Options
  • 17-05-2007 7:27pm
    #1
    Registered Users 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 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 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 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 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