I am scraping a page and i am getting this result:
JavaScript
x
string(1) " "
string(15) " +0,25 pist.wit"
string(14) " +0,25 pist.br"
// and so on...
But i want a result like this:
JavaScript
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:
JavaScript
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.
JavaScript
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:
JavaScript
int(0)
string(0) ""
float(0.25)
string(7) "pist.br"
int(0)
string(7) "bol wit"