2

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?

CodeOverload
  • 47,274
  • 54
  • 131
  • 219
  • First you must define what you mean with "wrap" text. Because all the code should be wrapped, at least, with tags. Second, what do you mean without relying on new lines? Third and last, why is this tagged under php/regex? If you actually want some php code, can you show us what you have tried already please? – Francisco Presencia Apr 10 '12 at 03:23
  • 1
    The usual comment for people who want to parse html using regular expressions: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Basti Apr 10 '12 at 03:38
  • Curios what happens for `

    Text

    More text
    `
    – Jason McCreary Apr 10 '12 at 04:00

2 Answers2

2

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.

jlapoutre
  • 1,767
  • 18
  • 22
d_inevitable
  • 4,381
  • 2
  • 29
  • 48
1

Extract tokens from your string like for example: <div>, foo, </div>, <p>, bar, </p>, Unwrapped text. You can do this with regular expressions. Then

for each token do
    if token is opening tag
        push token on stack
    else if token is closing tag (and matching opening tag is ontop of stack)
        pop token from stack
    else if token is text and stack is not empty
        ignore token (continue)
    else if token is text and stack is empty
        wrap token with <span>

This will work for arbitrary nested XML-strings.

Basti
  • 3,998
  • 1
  • 18
  • 21