Re: Regex Help
| From: | Kevin Waterson | Date: | Tue, 11 Feb 2003 11:25:46 +0000 |
| Subject: | Re: Regex Help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-135183@lists.php.net to get a copy of this message | ||
This one time, at band camp,
"Lord Loh." <lordloh@hotpop.com> wrote:
> I am trying to make a link collector.
>
> after opening the desired page, I want to get all the hyperlinks on it...
OK, this is quick and nasty, but you can add sanity/error checking etc
as you please, but it shows you the concept..
<?php
// tell me its broken
error_reporting(E_ALL);
class grabber{
function grabber(){
}
function getLinks(){
// regex to get the links
$contents = $this->getUrl();
preg_match_all("|http:?([^\"' >]+)|i",
$contents, $arrayoflinks);
return $arrayoflinks;
}
// snarf the url into a string
function getUrl(){
return file_get_contents($_POST['url']);
}
} // end class
?>
<html>
<body>
<h1>Link Grabber</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<table>
<tr><td>URL:</td><td><input type="text" name="url"
maxlength="100" size="50" value="http://www.php.net"></td></tr>
<tr><td><input type="submit" value="Grab
em"></td></tr>
</table>
</form>
<?php
// check something was posted
if(!isset($_POST['url']) || $_POST['url']=='')
{
// or tell them to
echo 'Please Enter a Valid url';
}
else
{
// create a new grabber
$grab = new grabber;
// snarf the links
$arr=$grab->getLinks();
// check the size of the array
if(sizeof($arr['0'])=='0')
{
// echo out a little error
echo 'No links found in file '.$_POST['url'];
}
else
{
// filter out duplicates and print the results
foreach(array_unique($arr['0']) as $val){ echo '<a
href="'.$val.'>'.$val.'<br />'; }
}
}
?>
</body></html>
enjoy,
Kevin
--
______
(_____ \
_____) ) ____ ____ ____ ____
| ____/ / _ ) / _ | / ___) / _ )
| | ( (/ / ( ( | |( (___ ( (/ /
|_| \____) \_||_| \____) \____)
Kevin Waterson
Port Macquarie, Australia