开发者

PHP regex: match lines that do not start with an HTML tag?

开发者 https://www.devze.com 2023-02-22 06:26 出处:网络
This problem seems simple enough, and yet I cannot come ac开发者_JS百科ross an example of it on the web...

This problem seems simple enough, and yet I cannot come ac开发者_JS百科ross an example of it on the web...

I am trying to create a preg_replace that takes a line at a time, and puts <p> tags around it if it does not begin with an HTML character already.

For instance, this paragraph becomes <p>this paragraph</p>

while

<code>this content</code> remains the same, with no <p> tags being put around it.

What would be the simplest way to achieve this?


You could try a (?!..) negative assertion after the ^ line start marker:

$text = preg_replace('#^(?!<[a-z]).*$#m', '<p>$0</p>', $text);

It doesn't test for a complete tag however. It assumes a tag is present when it sees <x already.

0

精彩评论

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