Ryan Ryan - 2 months ago 5
PHP Question

Wrap unwrapped text in <span>

Example:

<div>foo</div>
<p>bar</p>
Unwrapped text


What i want:

<div>foo</div>
<p>bar</p>
<span>Unwrapped text</span>


How to achieve this without relying on new lines?

Answer

I would not use regular expressions for html.

You can do it with phpQuery

$doc = phpQuery::newDocument($html);
$doc->contents()->not($doc->children())->wrap("<span>");
$html = $doc->html();

Didn't try it though.