I am using php 7.4.1 and "fabpot/goutte": "^3.3".
I am having the following script:
<?php
require_once '../vendor/autoload.php';
use Symfony\Component\DomCrawler\Crawler;
use Goutte\Client;
try {
$resArr = array();
$tempArr = array();
$url = "https://edikte.justiz.gv.at/edikte/ex/exedi3.nsf/0/19dd135274ceb842c12586390028507e?OpenDocument&f=1&bm=2";
// get page
$client = new Client();
$content = $client->request('GET', $url)->html();
$crawler = new Crawler($content, null, null);
$table = $crawler->filter('#diveddoc > div:nth-child(2) > table')->first()->closest('table');
$table->filter('tr')
->each(function (Crawler $tr) use (&$firm, &$resArr, &$tempArr) {
$val = addScrappedTextToArr($tr, 'PLZ/Ort:');
list($tempArr, $val) = checkNullAddArr($val, "plz_ort", $tempArr);
$val = addScrappedTextToArr($tr, 'Objektgröße:');
list($tempArr, $val) = checkNullAddArr($val, "objektGroesse", $tempArr);
});
array_push($resArr, $tempArr);
var_dump($resArr);
} catch (Exception $e) {
report($e);
}
function checkNullAddArr($val, $key, $tempArr)
{
if (!is_null($val)) {
$tempArr[$key] = $val;
$val = null;
}
return array($tempArr, $val);
}
function addScrappedLinkToArr(Crawler $tr, $scrapVal)
{
if (strpos($tr->text(), $scrapVal) !== false) {
$val = "https://edikte.justiz.gv.at" . trim($tr->filter('td > a')->attr("href"));
return $val;
}
}
function addScrappedTextToArr(Crawler $tr, $scrapVal)
{
/*
if ($tr->filter('td')->count() >= 2) {
$label = $tr->filter('td.tlabel')->text();
*/
if (strpos($tr->text(), $scrapVal) !== false) {
$val = trim(str_replace([$scrapVal], "", $tr->text()));
return $val;
// array_push($resArr, $val);
}
// }
// return $arr;
}
As you can see the output is the following for the array-key objectGroesse:
However, I would like to get the yellow text instead of the red underlined text:
As I am simply trying to match all the strings the word Objektgröße: is found in the larger text and matched.
I tried the following by simply rewriting the function addScrappedTextToArr() to filter on the label:
function addScrappedTextToArr(Crawler $tr, $scrapVal)
{
if ($tr->filter('td')->count() >= 2) {
$label = $tr->filter('td.tlabel')->text();
if (strpos($tr->text(), $scrapVal) !== false) {
$val = trim(str_replace([$scrapVal], "", $tr->text()));
return $val;
// array_push($resArr, $val);
}
}
// return $arr;
}
However I get the following error:
The current node list is empty.
Any suggestions how to fix my above bug?
I appreciate your replies!

