jjacquay712 Posted January 21, 2009 Share Posted January 21, 2009 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 More sharing options...
flyhoney Posted January 21, 2009 Share Posted January 21, 2009 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); ?> Link to comment https://forums.phpfreaks.com/topic/141828-solved-regex-link-grabber/#findComment-742575 Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Author Share Posted January 21, 2009 hmmm... the regex from that site didn't work Link to comment https://forums.phpfreaks.com/topic/141828-solved-regex-link-grabber/#findComment-742585 Share on other sites More sharing options...
flyhoney Posted January 21, 2009 Share Posted January 21, 2009 Yeah, here is a simpler one: $pattern = '/<a[^>]+href="([^"]+)"[^"]*>/is'; $subject = file_get_contents($_GET['page']); preg_match_all($pattern, $subject, $array); print_r($array[1]); Link to comment https://forums.phpfreaks.com/topic/141828-solved-regex-link-grabber/#findComment-742590 Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Author Share Posted January 21, 2009 thanks, ill try it Link to comment https://forums.phpfreaks.com/topic/141828-solved-regex-link-grabber/#findComment-742595 Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Author Share Posted January 21, 2009 Thanks a lot! it works perfectly! Link to comment https://forums.phpfreaks.com/topic/141828-solved-regex-link-grabber/#findComment-742599 Share on other sites More sharing options...
nrg_alpha Posted January 21, 2009 Share Posted January 21, 2009 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); Link to comment https://forums.phpfreaks.com/topic/141828-solved-regex-link-grabber/#findComment-742609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.