Auto detect URLs and make them links

Discussion in 'PHP' started by Kyriakos, Oct 2, 2009.

  1. #1
    hi,

    is there a simply way to detect URLs with PHP and make them links? i have seen this in Facebook comments and in many other websites.

    thanks in advance
     
    Kyriakos, Oct 2, 2009 IP
  2. jpinheiro

    jpinheiro Peon

    Messages:
    1,211
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    0
    #2
    what do you mean by "make them links" you want it to check if there is something written like this

    "google.com" if it sees that then it turns into <a href="google.com">google.com</a> ?
     
    jpinheiro, Oct 2, 2009 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    yes. if in the text is a url like "bla bla bla www.something.com bla bla bla" i want to turn this to "bla bla bla <a href="http://www.something.com" target="_blank">www.something.com</a> bla bla bla"
     
    Kyriakos, Oct 2, 2009 IP
  4. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    
    $string = " "; // you'll need to find a method to load the string with your text, pending on your source, it might be as simple as $post_data or some other variable already in use
    
    $search = "/(http[s]*:\/\/[\S]+)/";
    $replace = "<a href='\${1}'>\${1}</a>";
    $output = preg_replace($search, $replace, $string);  
    
    
    PHP:
     
    pixmania, Oct 2, 2009 IP
  5. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    thank you man.
     
    Kyriakos, Oct 2, 2009 IP