Detecting url in a text

Discussion in 'Programming' started by SGBoise, Aug 28, 2009.

  1. #1
    Hi guys,

    I want to do something like youtube where when you type a url in the description it will automatically turn it into a hyperlink.

    Any advice on getting started?
     
    SGBoise, Aug 28, 2009 IP
  2. Nerv

    Nerv Active Member

    Messages:
    568
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    70
    #2
    try this:

    function parse_links($str)
    {
    $str = str_replace('www.', 'http://www.', $str);
    $str = preg_replace('|http://([a-zA-Z0-9-\./]+)|', '<a href="http://$1">$1</a>', $str);
    $str = preg_replace('/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $str);
    return $str;
    }
    ?>
    <span id="more-50"></span>
    Code (markup):
     
    Nerv, Aug 28, 2009 IP
    SGBoise likes this.
  3. SGBoise

    SGBoise Peon

    Messages:
    647
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. I'll give it a try.
     
    SGBoise, Aug 28, 2009 IP
  4. AustinQPT

    AustinQPT Member

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    That won't work if they post a url with just http:// or with http://www. because the http://www. will turn into http://http://www.

    try this...

    
    $str = $variable; //where $variable is the variable of the post or description
    $str = str_replace('http://', 'www.', $str);
    $str = str_replace('http://www.', 'www.', $str);
    $str = str_replace('www.', 'http://www.', $str);
    $str = preg_replace('|http://([a-zA-Z0-9-\./]+)|', '<a href="http://$1">$1</a>', $str);
    $str = preg_replace('/(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $str);
    $str = trim($str);
    echo "".$str.""; //prints the variable of the post with the new link added.
    
    Code (markup):
    Only problem is that if they don't have www. or http:// or http://www. or if they have https:// or another link it will not work sorry I can't test it at the moment I am on vacation and using my phone...don't tell.
     
    AustinQPT, Aug 29, 2009 IP
    SGBoise likes this.
  5. Nerv

    Nerv Active Member

    Messages:
    568
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    70
    #5
    AustinQPT You are right, good code btw ;)
     
    Nerv, Aug 30, 2009 IP
  6. SEO queries

    SEO queries Peon

    Messages:
    124
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    Good one..I will also give it a try.
     
    SEO queries, Aug 31, 2009 IP
  7. shinji_kidou

    shinji_kidou Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    good answer, its worked
     
    shinji_kidou, Sep 3, 2009 IP
  8. JoeDaniels00

    JoeDaniels00 Well-Known Member

    Messages:
    222
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    113
    #8
    lol
    lol
    olo
     
    JoeDaniels00, Sep 4, 2009 IP
  9. SGBoise

    SGBoise Peon

    Messages:
    647
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is a nice code. it took me 5 minutes to add it to my script. AustinQPT definitely saved me a lot of hours.
     
    SGBoise, Sep 4, 2009 IP
  10. arvinarvin

    arvinarvin Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanks for the code.

    bookmarked for future use
     
    arvinarvin, Sep 7, 2009 IP