Finde Wert im Array nicht / Der Wert ist aber vorhanden

Hallo liebe Symcon Gemeinde,

ich brauche mal wieder Eure Hilfe.

Und zwar möchte ich gerne Werte, von einer Homepage in Variablen schreiben.
Da aber die Werte, immer verschiedene Schlüssel haben können, muss ich danach suchen können.
Das ist aber mein Problem, ich habe schon viele Skripte ausprobiert, der Wert wird aber nie gefunden, obwohl er vorhanden ist.

Hier mein Skript mit dem Ergebnis:

<?php

$curl = curl_init('https://www.wgmn-hamburg.de/daten-2/ReportAktuelleMesswerte-WA-2.html'); //Warndienst Binnenhochwasser Hamburg
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/61.0.3163.100 Safari/537.36');

$page = curl_exec($curl);
//print_r($page);

if(curl_errno($curl)) // check for execution errors
{
    echo 'Scraper error: ' . curl_error($curl);
    exit;
}
curl_close($curl);


$content = get_mark($page, '<td>*</td>');
$truebung = str_replace(' ', '', get_mark($page, '<td>*</td>' )[5]);  // ' ' = Leerzeichen ersetzen durch ''

//var_dump ($content);

$temp3 = ($content[4]);

var_dump ($temp3).PHP_EOL;

    $pos = array_search('pH-Wert', $content, true);
 
    // Abfrage: Wurde "april" im Array gefunden?
    // Falls ja, hat $pos einen Wert zwischen 0 und 3 (Schlüssel des ersten und letzten Werts im Array)
    // Falls nein, ist $pos false.
    // Bei der Bedingung muss darauf geachtet werden, immer "=== false" und NICHT "== false" zu schreiben, da "== false" auch
    // dann true ergibt, wenn der gefundene Wert den Schlüssel 0 hat.
    if ($pos===false) {
        echo('"pH-Wert" wurde nicht im Array gefunden.').PHP_EOL;
    } else {
        echo('"pH-Wert" hat im Array den Schlüssel '.$pos).PHP_EOL;
    }


var_dump ($content);


function get_mark($string,$mark) {
	$ausgabe = array();
	$template = explode("*",$mark);
	$mark = $template[0];
	$end = $template[1];
	$string = strstr($string,$mark);
	$temp = explode($mark,$string);
	$a = 1;
	foreach ($temp as $tempx) {
		$tempx = explode($end,$tempx);
		$tempx = $tempx[0];
		if ($tempx) {
			array_push ($ausgabe,$tempx);
		}
	}
	return $ausgabe;
}

Und hier das Ergebnis davon:

string(11) "
pH-Wert
"
---------------------------------------------------------------
"pH-Wert" wurde nicht im Array gefunden.
----------------------------------------------------------------
array(32) {
  [0]=>
  string(41) "
Wandsbeker Allee - Aktuelle Messwerte
"
  [1]=>
  string(8) "
&nbsp;"
  [2]=>
  string(8) "
&nbsp;"
  [3]=>
  string(20) "
05.06.2025 07:00
"
  [4]=>
  string(11) "
pH-Wert
"
  [5]=>
  string(13) "
      8.3
"
  [6]=>
  string(8) "
ohne
"
  [7]=>
  string(20) "
05.06.2025 07:00
"
  [8]=>
  string(11) "
Trübung
"
  [9]=>
  string(13) "
     23.0
"
  [10]=>
  string(7) "
FNU
"
  [11]=>
  string(20) "
05.06.2025 07:00
"
  [12]=>
  string(20) "
Wassertemperatur
"
  [13]=>
  string(13) "
     20.5
"
  [14]=>
  string(6) "
°C
"
  [15]=>
  string(20) "
05.06.2025 07:00
"
  [16]=>
  string(23) "
Sauerstoffsättigung
"
  [17]=>
  string(13) "
    103.9
"
  [18]=>
  string(5) "
%
"
  [19]=>
  string(20) "
05.06.2025 07:00
"
  [20]=>
  string(27) "
Sauerstoffkonzentration
"
  [21]=>
  string(13) "
      9.3
"
  [22]=>
  string(8) "
mg/l
"
  [23]=>
  string(16) "
WGMN-Service
"
  [24]=>
  string(8) "
&nbsp;"
  [25]=>
  string(8) "
&nbsp;"
  [26]=>
  string(70) "
Messstation Wandsbeker Allee an der Wandse an der Wandsbeker Allee
"
  [27]=>
  string(8) "
&nbsp;"
  [28]=>
  string(8) "
&nbsp;"
  [29]=>
  string(158) "
Die aktuellen Messwerte der Station Wandsbeker Allee – täglich mehrmals frisch! Wir stellen die Messdaten für Sie morgens, mittags und nachmittags bereit.
"
  [30]=>
  string(8) "
&nbsp;"
  [31]=>
  string(8) "
&nbsp;"
}

Vielen Dank für die Hilfe.

Hein09

Das sieht so aus, als wenn der Eintrag im Array nicht direkt „pH-Wert“ ist, sondern davor und danach Zeilenumbrüche sind. Damit greift die Prüfung auf Gleichheit nicht. Der Check wird also ein bisschen komplizierter. Mein Ansatz wäre es mit PHP: array_filter - Manual ranzugehen

Du könntest auch beim bauen des content arrays ein trim() nutzen um Leerzeilen und neue Zeilen direkt zu entfernen:

$tempx = trim($tempx[0]);
if ($tempx) {
	array_push($ausgabe, $tempx);
}
1 „Gefällt mir“

Mit einem einfachen

foreach ($content as &$wert) {
    $wert = trim($wert);
}

funktioniert es jetzt,

Vielen Dank für die Hilfe.
Hein09

1 „Gefällt mir“

es funktioniert leider nicht richtig.
Die Wörter die ein Umlaut haben, werden nicht gefunden , alle anderen ja.

Habt Ihr dafür, auch noch einen Tipp, für mich?

Hein09

Da ich das zerlegen von HTML zu hassen gelernt habe, habe ich hier eine Lösung mit xPath für dich :wink:

$html = new DOMDocument('1.0','charset=WINDOWS-1252');
$error = @$html->loadHTMLFile('https://www.wgmn-hamburg.de/daten-2/ReportAktuelleMesswerte-WA-2.html');
if (!$error) {
    echo "error";
    return false;
}
$Indexes =[
    'pH-Wert',
    'Wassertemperatur',
    'Sauerstoffsättigung',
    'Sauerstoffkonzentration',
    'Trübung',
];
$Result =[];
foreach ($Indexes as $index){
    $xpath = new DOMXPath($html);
    $query = '//*[text()[contains(.,"'.$index.'")]]';
    $nodes = $xpath->query($query);
    if (count($nodes)){
        $Result[$index] = (float)(trim($nodes[0]->nextSibling->nextSibling->nodeValue));
    } else {
        echo 'Index '. $index . ' nicht gefunden!'.PHP_EOL;
    }
}
var_dump($Result);

Edit:

Ich vermute es liegt am charset=WINDOWS-1252 der Seite.
Michael

Moin Nall-Chan,

vielen Dank für Deine Hilfe, bei ausführen des Skriptes bekomme ich folgende Fehlermeldung:


Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "textContent" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "textContent" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "textContent" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "textContent" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "nextSibling" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21

Warning: Attempt to read property "textContent" on null in /mnt/data/symcon/scripts/32969.ips.php on line 21
array(5) {
  ["pH-Wert"]=>
  float(0)
  ["Wassertemperatur"]=>
  float(0)
  ["Sauerstoffsättigung"]=>
  float(0)
  ["Sauerstoffkonzentration"]=>
  float(0)
  ["Trübung"]=>
  float(0)
}

Hein09

Das er gar keinen bei dir findet, wundert mich.
Ich habe das Script oben um eine Fehlerprüfung ergänzt.
Hier klappt das wunderbar…
image

Kannst du mal schauen ob das Util Control / Utils Handler (unter Kern Instanzen) bei „Umlaute korrigieren“ das Script mit aufführt?
Michael

Wenn ich „Umlaute korrigieren“ anklicke , bekomme ich diese Meldung:
Screenshot 2025-06-05 145705

Hier mit dem neuen Skript:

Index pH-Wert nicht gefunden!
Index Wassertemperatur nicht gefunden!
Index Sauerstoffsättigung nicht gefunden!
Index Sauerstoffkonzentration nicht gefunden!
Index Trübung nicht gefunden!
array(0) {
}

Hein09