开发者

Matching the first <p> that does not start with an image in regexp

开发者 https://www.devze.com 2022-12-14 06:39 出处:网络
In PHP, I\'m trying to match the first paragraph tag that is not immediately followed by an <img> tag, and add a class to that paragraph tag.

In PHP, I'm trying to match the first paragraph tag that is not immediately followed by an <img> tag, and add a class to that paragraph tag.

For example, it would add a class to:

<p>Text</p>

and

<p><strong>Strong text</strong></p>

but not:开发者_如何学JAVA

<p><img src="" /></p>

Here's what I have so far which successfully adds a class to the first paragraph tag, but it doesn't skip until it finds a paragraph tag that doesn't immediately contain an image:

preg_replace('/<p>/','<p class="first">', $text, 1);

Thoughts? Apologies if that doesn't make sense.


/<p>(?!<img)/<p class="first">/
0

精彩评论

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