Getting URLs from a text – PHP

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: ¬†
  • Without www:
  • With www and http:
  • 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
			return $match[0];
		return 0;