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
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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.