Kann man auch mit preg_match_all() machen.
PHP
$html
= '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Foo</title>
</head>
<body>
<div class="item_now">Gurke</div>
<div class="item_now"></div>
<div>
<div>
<div>
<div class="item_now">Tennisschläger</div>
<div class="item_now">Fußball</div>
</div>
<div class="item_now">Tennisball</div>
</div>
</div>
<DIV CLASS="ITEM_NOW">TENNISBALL</DIV>
</body>
</html>
';
/**
* Pattern break down:
* / delimiter
* \<div class="item_now"\>
* ( start capture
* .* one or none of any sign
* ) end capture
* \<\/div\>
* / delimiter
* i case insensitive (html and css is case insensitive)
*/
$pattern = "/\<div class=\"item_now\"\>(.*)\<\/div\>/i";
$matchesCount = preg_match_all($pattern, $html, $matches);
if ($matchesCount > 0) {
echo "Matches: {$matchesCount}\r\n";
// $matches[0] contains matches
// $matches[1] contains captured content
foreach ($matches[1] as $match) {
echo "'{$match}'\r\n";
}
} else {
echo "Matches: 0";
}
Alles anzeigen
Code
// matches output
array (
0 =>
array (
0 => '<div class="item_now">Gurke</div>',
1 => '<div class="item_now"></div>',
2 => '<div class="item_now">Tennisschläger</div>',
3 => '<div class="item_now">Fußball</div>',
4 => '<div class="item_now">Tennisball</div>',
5 => '<DIV CLASS="ITEM_NOW">TENNISBALL</DIV>',
),
1 =>
array (
0 => 'Gurke',
1 => '',
2 => 'Tennisschläger',
3 => 'Fußball',
4 => 'Tennisball',
5 => 'TENNISBALL',
),
)
Alles anzeigen