wwfc_barmy_army Posted April 19, 2009 Share Posted April 19, 2009 Hello. I'm trying some PHP scraping, as i've never done anything like this before and i'm still reasonably new to php. I've got it so it can display all links using this code: foreach($html->find('a') as $e) echo $e->href . '<br>'; I want it so it only shows links to a certain domain. Eg. only show links to http://mydomain.com or http://www.mydomain.com. any ideas how i could do this? Some kind of preg match? ??? Thanks. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 $domain = 'http://www.google.com'; $link = preg_match('/http\:\/(\/www\.|\/)'.$domain.'\..*?/i', $link); Something like that...untested though , so it'll probably need some messing with. Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted April 19, 2009 Author Share Posted April 19, 2009 Is there somewhere that explains how to build a preg_match? Is there an if statement i can use for that, eg: foreach($html->find('a') as $e) if(Starts with http://mydomain){ echo $e->href . '<br>'; } Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 19, 2009 Share Posted April 19, 2009 I would approach it like this: $dom = new DOMDocument; @$dom->loadHTMLFile('http://www.somesite.com/'); $xpath = new DOMXPath($dom); $aTag = $xpath->query('//a[@href="http://mydomain.com" or @href="http://www.mydomain.com"]'); foreach ($aTag as $val) { echo $val->nodeValue . "<br />\n"; } Assuming the site in question has an anchor tag with the attribute href containing either http://mydomain.com or http://www.mydomain.com, those a tags will be stored within the array $aTag. Then, with a foreach loop, simply extract the link text via nodeValue Quote Link to comment Share on other sites More sharing options...
alphanumetrix Posted April 19, 2009 Share Posted April 19, 2009 preg_match is used for regular expressions. If you want to learn more about it, I suggest you just google "create regular expression" However, instead of looking for a regular expression, you should be able to use strpos, and it should fix your little problem. $site = 'http://mysite.com'; foreach($html->find('a') as $e) { if ( strpos($e->href, $site) == true ) { echo $e->href . '<br>'; } } Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted April 19, 2009 Author Share Posted April 19, 2009 preg_match is used for regular expressions. If you want to learn more about it, I suggest you just google "create regular expression" However, instead of looking for a regular expression, you should be able to use strpos, and it should fix your little problem. $site = 'http://mysite.com'; foreach($html->find('a') as $e) { if ( strpos($e->href, $site) == true ) { echo $e->href . '<br>'; } } Thanks it worked. Couldn't get it to work with 'http://mydomain.com' had to just use 'mydomain.com' any reason for this? Thanks. Quote Link to comment Share on other sites More sharing options...
alphanumetrix Posted April 20, 2009 Share Posted April 20, 2009 My bad. You will have to use === for getting the boolean with strpos. So try this: $site = 'http://mysite.com'; foreach($html->find('a') as $e) { if ( strpos($e->href, $site) === true ) { echo $e->href . '<br>'; } } 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.