Answer the question
In order to leave comments, you need to log in
XPath: how to find all nodes of the current variable?
Hello!
WordPress has a $content variable.
I need to wrap EVERY root node inside this variable in a div tag, so it looks like this:
<div class="parent"><i>Расписание</i></div>
<div class="parent"><i>Расписание</i></div>
<div class="parent"><i>Расписание</i></div>
$cont = mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' );
$dom = new DomDocument();
$dom->loadHTML( $content );
$xpath = new DomXPath( $dom );
foreach( $xpath->query( './/*' ) as $element ) {
return "<div class=\"parent\">" . $element->C14N() . PHP_EOL . "</div>" . PHP_EOL;
}
<div class="parent">
<i>Расписание</i>
<i>Расписание</i>
<i>Расписание</i>
</div>
Answer the question
In order to leave comments, you need to log in
<?php
$content = '
<i>Расписание</i>
<i>Расписание</i>
<i>Расписание</i>';
$cont = mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' );
$dom = new DomDocument();
$dom->loadHTML( $content );
$xpath = new DomXPath( $dom );
foreach( $xpath->query( './/i' ) as $element ) {
echo "<div class=\"parent\">" . $element->C14N() . PHP_EOL . "</div>" . PHP_EOL;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question