Jump to content

[SOLVED] PHP Scrape and preg match?


wwfc_barmy_army

Recommended Posts

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

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

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>';
    }

}


 

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.

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.