Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract URLs from text in PHP

Tags:

html

regex

php

I have this text:

$string = "this is my friend's website http://example.com I think it is coll"; 

How can I extract the link into another variable?

I know it should be by using regular expression especially preg_match() but I don't know how?

like image 580
ahmed Avatar asked May 26 '09 14:05

ahmed


People also ask

How to get url from string in PHP?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

What is parse_ url in PHP?

The parse_url() function is an inbuilt function in PHP which is used to return the components of a URL by parsing it. It parses an URL and return an associative array which contains its various components. Syntax: parse_url( $url, $component = -1 )


2 Answers

Probably the safest way is using code snippets from WordPress. Download the latest one (currently 3.1.1) and see wp-includes/formatting.php. There's a function named make_clickable which has plain text for param and returns formatted string. You can grab codes for extracting URLs. It's pretty complex though.

This one line regex might be helpful.

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match); 

But this regex still can't remove some malformed URLs (ex. http://google:ha.ckers.org ).

See also: How to mimic StackOverflow Auto-Link Behavior

like image 101
Nobu Avatar answered Sep 21 '22 02:09

Nobu


I tried to do as Nobu said, using Wordpress, but to much dependencies to other WordPress functions I instead opted to use Nobu's regular expression for preg_match_all() and turned it into a function, using preg_replace_callback(); a function which now replaces all links in a text with clickable links. It uses anonymous functions so you'll need PHP 5.3 or you may rewrite the code to use an ordinary function instead.

<?php   /**  * Make clickable links from URLs in text.  */  function make_clickable($text) {     $regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';     return preg_replace_callback($regex, function ($matches) {         return "<a href=\'{$matches[0]}\'>{$matches[0]}</a>";     }, $text); } 
like image 37
Mikael Roos Avatar answered Sep 23 '22 02:09

Mikael Roos