For this kind of tasks regular expressions are the key. With this function i am able to get URLs in the most common formats:

  • With www: www.google.com
  • Without www: http://google.com
  • With www and http: http://www.google.com
  • And even with https.

Take a look at it:

/**
 * Gets the URLs contained in a text.
 *
 * @access public
 * @param string $text text
 * @return array of URLs
 */
public function getURL($text){
    //getting the URLs with a regular expresion
    preg_match_all('#\b(https?://|www[.]?)[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $text, $match);

    //if there are URLs
    if(!empty($match)){
        return $match[0];
    }
    return 0;
}