SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: text 2 link (?)

  1. #1
    SitePoint Member
    Join Date
    Nov 2001
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    text 2 link (?)

    Hi!

    I just wanted to know how i can covert text to link?

    Could anyone help me please.

    Thanks..

  2. #2
    Next stop: PHP! Marina's Avatar
    Join Date
    Apr 2001
    Location
    Sweden
    Posts
    692
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this tutorial/script a while ago (but I haven't tested it yet...):

    Automatic Text to Links

    Are you doing something where a user inputs information into a form? Is this for a comments program, or a forum that you are creating? Well, adding this little function is very nice. You see, if someone types in a URL like http://www.newbienetwork.net/ without adding the <a href> tag around it, it will simply appear as normal text. However, if you pass the content of that text field through this function, it will spit out new content with all the links put into <a href> tags, and emails with the mailto: addon. Neat huh?
    PHP Code:
    function text_to_links ($data='') {
            if(empty(
    $data)) { return $data; }
            
    $lines split(" ",$data);
            while ( list (
    $key,$line) = each ($lines)) {
                    
    $line eregi_replace("([ ]|^)[url]www.[/url]","http://www.",$line);
                    
    $text eregi_replace("([url]http://[/url][^ ) ]+)","<A href="1" target="_blank">1</A>",$line);
                    
    $text eregi_replace("([-a-z0-9_]+(.[_a-z0-9-]+)*@([a-z0-9-]+(.[a-z0-9-]+)+))","<AHREF="1",$text'>mailto:1">1</A>",$text);
                    
    $newText .= $text;
            }
            return 
    $newText;

    So, to use this you would simply do this:
    PHP Code:
    $text = <text from a textbox, or textarea, or something>
    $test text_to_links($text); 

  3. #3
    SitePoint Member
    Join Date
    Nov 2001
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yah, i know that tutorial, i think there is an error somewhere in the code, it doesn't work

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of Hi mom put <a href="/place/to/go">Hi mom</a>
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Member
    Join Date
    Nov 2001
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Defender ..

    Are you joking?

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a function made from some code made by Anarchos

    Code:
    function link_it ($str) {
    
        $str = preg_replace("/(https?:\/\/|ftp:\/\/|mailto:|www\.)([a-z0-9\._@]+)/i","<a href=\"\\1\\2\">\\2</a>",$str); 
    
        $str = preg_replace("/href=\"www\./i","href=\"http://www.",$str);
    
        return $str;
    
    }
    Use it like this:

    PHP Code:
    $Text "You can find SitePoint at http://www.sitepoint.com";

    $NewText link_it($Text);

    echo 
    $NewText
    I could not test this, but it should work!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    SitePoint Member
    Join Date
    Nov 2001
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks seanf..

  8. #8
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by osam
    Defender ..

    Are you joking?
    Yes.
    hence the after my post.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •