Jump to content

[SOLVED] Regex Link Grabber


jjacquay712

Recommended Posts

Here is my script with the regex:

 

<?php
$code = file_get_contents($_GET['page']);
preg_match_all('/href=\"(.*)\"/', $code, $array);
echo '<pre>';
print_r($array);
echo '</pre><br />';
foreach ($array[1] as $lol => $val) {
echo $val . '<br />';
}
?>

 

when i want to get the links from google.com the regex returns garbage. I'm not to good at regex, are there any problems you can see with it? i also want it to match when the <A> tag is capitalized. Thanks, jjacquay712

Link to comment
https://forums.phpfreaks.com/topic/141828-solved-regex-link-grabber/
Share on other sites

This is a question for the regex sub-forum. 

 

Here is a site with some thorough regex examples: http://regexlib.com/DisplayPatterns.aspx?cattabindex=7&categoryId=8

 

<?php
$code = file_get_contents($_GET['page']);
preg_match_all('(?<HTML><a[^>]*href\s*=\s*[\"\']?(?<HRef>[^"\'>\s]*)[\"\']?[^>]*>(?<Title>[^<]+|.*?)?</a\s*>)/', $code, $array);
print_r($array);
?>

Using flyhoney's URL as an example:

 

$code = file_get_contents('http://regexlib.com/DisplayPatterns.aspx?cattabindex=7&categoryId=8');
preg_match_all('#href=[\'"]([^\'"]+)[\'"]#i', $code, $matches);
echo '<pre>'.print_r($matches[1], true);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.