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.
精彩评论