Answer the question
In order to leave comments, you need to log in
Why doesn't the xpath check work?
Good afternoon!
It is necessary in a loop (when iterating over elements) to check whether the element fits the xpath condition.
In this case, does the class contain foo
.
If it contains - skip it, if not - add it to the variable.
I do it like this:
$content = '';
foreach($entry as $element) {
// Вот здесь я спрашиваю, текущий элемент в цикле
// подходит под условие xpath?
if ($xpath->evaluate('self::div[contains(@class, \'foo\')]', $element) === true) {
continue;
}
else {
$content .= $dom->saveHTML($element);
}
}
echo "$content";
Answer the question
In order to leave comments, you need to log in
$entry = $xpath->query('//div[contains(@id, \'entry\')]/div');
$content = '';
foreach($entry as $element) {
if(! $xpath->evaluate("contains(@class, 'foo')", $element)) {
$content .= $dom->saveHTML($element) . PHP_EOL;
}
}
echo $content;
//<div class="world bar">bla bla bla</div>
//<div class="two bar">bla bla bla</div>
//<div class="paper bar">bla bla bla</div>
//<div class="boo bar loo">bla bla bla</div>
https://3v4l.org/FhNUp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question