Skip to content
Advertisement

How do i filter the output of a scraper? (PHP) [closed]

I am scraping a page and i am getting this result:

string(1) " "
string(15) " +0,25 pist.wit"
string(14) " +0,25 pist.br"
// and so on...

But i want a result like this:

0,25
0,25
//and so on...

So technically i want to filter the prices (without + signs) and the bread names (pist.wit etc.) Does someone know how to do this? Here is my code:

 public function onRun()
{
    $client = new Client();
    $crawler = $client->request('GET', 'http://www.sandwich-express.nl/online-bestellen/');
    $crawler->filter('tr')->each(function ($node) {
        if(sizeof($node->filter('.table-spacing')) > 0)
            var_dump('nieuwe headers next TR');
        $node->filter('tr.colomn_text td')->each(function ($node) {
            var_dump($node->text());
        });
    });

}

Advertisement

Answer

I assume you meant that you have the price and names as 2 values as noted below.

public function onRun()
{
    $client = new Client();
    $crawler = $client->request('GET', 'http://www.sandwich-express.nl/online-bestellen/');
    $crawler->filter('tr')->each(function ($node) {
        if(sizeof($node->filter('.table-spacing')) > 0)
            var_dump('nieuwe headers next TR');
        $node->filter('tr.colomn_text td')->each(function ($node) {
            $name = trim($node->text());
            $price = 0;
            if(0 === strpos($name, '+')) {
                $names = explode(' ', $name);
                $price = floatval(str_replace(['+', ','], ['', '.'], array_shift($names)));
                $name = implode(' ', $names);
            }
            var_dump($price, $name);
        });
    });
}

Result:

int(0)
string(0) ""
float(0.25)
string(7) "pist.br"
int(0)
string(7) "bol wit"
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement