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 Quote Link to comment 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); ?> Quote Link to comment 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 Quote Link to comment 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]); Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Author Share Posted January 21, 2009 thanks, ill try it Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Author Share Posted January 21, 2009 Thanks a lot! it works perfectly! Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.