开发者

Matching two sections same number of times

I'm searching for code to parse the following:

some texttext

I need to remove unnecessary <span> occurrences, so that output is:

some texttext

I wrote a regex, which does this once:

/[^<]*</SP开发者_如何学运维AN>/i

How do I make this work same number of times on both <span> and </span>?


$result = preg_replace(
    '%(?<=<span>)        # Assert that there is a directly preceding span tag
    <span>               # Match a span tag
    ((?:(?!</?span>).)*) # Match the contents of the tag only if they do not include another span tag
    </span>              # Match a closing span tag
    (?=</span>)          # Assert that there is a directly following span tag
    %six', 
    '\1', $subject);

will work on your example, but it has to be applied twice because it removes one "layer" of nested span tags per iteration.

So, with arbitrarily nested tags, you'd need to call this once for each level of nesting.


You could try to see if a <span> is directly followed by another <span> and its matching </span> is directly prepended by another </span>.

But you cannot actually say that that span will be useless, because markup can be added to those specific spans. If there isn't any markup, the last remaining spans are useless as well and might just as well be removed too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜