I have this code:
$html = '<div class="container">A<div class="wrapper">B</div>C</div>'
$dom = new DOMDocument;
@$dom->loadHTML($html);
$xp = new DOMXPath($dom);
$links = $xp->query('//div[contains(@class,"container")]');
I want to make the DOMXPath query select the <div> element with class = "container" but i want it only to select the <div class="wrapper"></div> when it exists. So i want it to select <div class="container"> when <div class="wrapper"> doesn't exist, but when it does i want it to only select <div class="wrapper">.
Thanks in advance.
As first, you can count all
<div class="wrapper">elements via:if this returns int(0) it means, that no element with
wrapperclass has been found. With these information, we can easily modify your code to something like this: