S
S
Shimpanze2021-06-22 16:59:13
PHP
Shimpanze, 2021-06-22 16:59:13

How to return HTML instead of a text node?

Hello!

I have the word 'World' (a text node) received via xpath.
Through preg_splitI split it one letter at a time into an array.
How do I now wrap each letter in <span>and return the result back to '$dom' ?

Example:
- was: <h1>Мир</h1>
- became:

<h1><span>М</span><span>и</span><span>р</span></h1>


$html = '<h1>Мир</h1>';
$dom = new DomDocument();
@$dom->loadHTML( $html );
$xpath = new DomXPath( $dom );
$text_node = $xpath->query( '//h1/text()' )[0];
$chars_arr = preg_split( '//u', $text_node->nodeValue );

foreach ( $chars_arr as $char ) {
    // как здесь обернуть каждую букву в '<span>'
    // и вернуть результат обратно в $dom вместо текстовой ноды '$text_node'?
}


...without 'appendXML', of course.

I can’t think of something and that’s it ...
I would be grateful for any help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question