For a system I am building I am defining a general style
stored in LINKSTYLE
that should be applied to a
elements that are not yet styled (inline). I am not very experienced with the DOMDocument
or xpath
and I can't figure out what is going wrong.
Thanks to Gordon I've updated my code:
libxml_use_internal_errors(true);
$html = '<a href="#">test</a>'.
'<a href="#" style="border:1px solid #000;">test2</a>';
$dom = new DOMDocument();
$dom->loadHtml($html);
$dom->normalizeDocument();
$xpath = new DOMXPath($dom);
foreach($xpath->query('//a[not(@style)]') as $node)
$node->setAttribute('style','border:1px solid #000');
return $html;
With this updated code I receive no more errors, however the a
element does not get styled.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…