PHP Quick Script: Extract a link from a string
Have you ever needed to extract a link from a variable? Well with this little script it’s easy and you don’t have to learn regular expressions because it’s already included.
We’ve placed this script inside a function so you can use it multiple times throughout your site if you need to.
So let’s get going shall we. Let’s say we have a variable with the following string inside of it and we want to extract the link from the string…
[sourcecode language="PHP"]
$str = "<a href=\"http://foobar.com\"> |
Hello world Im a http://www.google.com |
Did you mean:http://www.google.co.za/
#hl=en&source=hp&biw=1362&bih=593&q=iza+tuto&aq=f&aqi=&aql=
&oq=&fp=2f13513b13d1ecfa";
[/sourcecode]
As you can see we have three links inside the string, one even has some query strings attached. For this we’ll use the PHP function preg_match_all(), let’s give you the function quickly to see how it works before we build the script.
[sourcecode language="PHP"]
preg_match_all($pattern_to_search_for, $inside_the_string, $array_of_all_matched_item_results);
[/sourcecode]
Regular expressions are beyond the scope of this post so we won’t be covering that here, the regex inside the function is the correct one to extract a link.
[sourcecode language="PHP"]
function extract_link($string) {
if (preg_match_all("`.*?((http|ftp)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i",$string,$matches)) {
return $matches[1];
} else {echo "No links inside the string!";}
}
[/sourcecode]
We wrap the preg_match_all function inside an if else statement to handle the error when there aren’t any links inside the string variable. In this example we only echo the fact, you can add your own code in the else statement to handle the error. Now that we have the function, we can call it to use it. Because it may have more than one result, we should place it in a foreach loop. In the example below we’ll only echo the links extracted from the string, but you can insert your own code to do with each result what you want.
[sourcecode language="PHP"]
// Put function results into a variable
$extracted = extract_link($str);
// Loop the array in case there are multiple results
foreach ($extracted as $link) {
// Handle each link in here
// We echo for this example
echo $link."<br />";
}
[/sourcecode]
That’s it, you can modify this funtion even more to fit your specific needs like inserting the links into a database. This little function also works great with a site crawler/spider, you use it on the page itself and if modified a little on the robots.txt page.
