faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 2 people (100%) answered Yes
Recently 2 of 2 people (100%) answered Yes

Entry

Looking for a way to grab a page of the net and see if it contains a specefic URL. Any ideas?

Aug 18th, 2004 05:52
Brenden Dawson, John Himer,


I do something similar to this already with cURL (http://curl.haxx.se/).
 cURL can be used on the command line (through PHP scripts) or with PHPs
cURL extensions (requires compiling PHP with cURL).
<example>
<?php
#pay attention to your forward slashes and 'dots' and escape accordingly
$link = "http:\/\/the\\.link\\.youre\/searching\\.for";
#the following does -not- need escaped
$url = "http://the.URL.you/will/be.searching";
$c = curl_init("$url");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$pagesource = curl_exec($c);
curl_close($c);
$pattern = "/<a href=.+".$link.".+?>/";
if (preg_match($pattern, $pagesource)) {
   echo "Found a link matching your requirements.<br>";
}
?>
</example>
The preg_match will match any opening href tag that contains the $url
you set.
eg: <a href="http://the.link.youre/searching.for"...>