2

例:

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

私が欲しいもの:

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

新しいラインに依存せずにこれを達成する方法は?

4

2 に答える 2

2

HTMLには正規表現を使用しません。

あなたはphpQueryでそれを行うことができます

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

しかし、それを試しませんでした。

于 2012-04-10T03:55:41.600 に答える
1

たとえば、、、、、、、、、のように文字列<div>からfooトークンを</div>抽出<p>します。これは正規表現で行うことができます。それでbar</p>Unwrapped text

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>

これは、任意のネストされた文字列に対して機能しXMLます。

于 2012-04-10T03:45:23.650 に答える