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. Link to comment https://forums.phpfreaks.com/topic/154793-solved-php-scrape-and-preg-match/ 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. Link to comment https://forums.phpfreaks.com/topic/154793-solved-php-scrape-and-preg-match/#findComment-814028 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>'; } Link to comment https://forums.phpfreaks.com/topic/154793-solved-php-scrape-and-preg-match/#findComment-814033 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 Link to comment https://forums.phpfreaks.com/topic/154793-solved-php-scrape-and-preg-match/#findComment-814034 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>'; } } Link to comment https://forums.phpfreaks.com/topic/154793-solved-php-scrape-and-preg-match/#findComment-814060 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. Link to comment https://forums.phpfreaks.com/topic/154793-solved-php-scrape-and-preg-match/#findComment-814127 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>'; } } Link to comment https://forums.phpfreaks.com/topic/154793-solved-php-scrape-and-preg-match/#findComment-814188 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.