Wrapping only paragraphs in a <p> tag
Posted: Sun Apr 03, 2011 1:55 am
My database stores articles in a plaintext format so when I retrieve it from the database there are no html tags around the actual paragraphs of the article.
I am trying to have a php function that takes the article and wraps only the paragraphs in a <p> tag.
The problem I am having is that I cannot make my regular expression work to only recognize the true paragraphs compared to code examples...
An example of before and after is below
Before:
[text]Aliquam erat volutpat. Morbi at justo lacus. Sed id dui felis, in fringilla urna. Aliquam erat volutpat. Maecenas nec diam ullamcorper nisi scelerisque vehicula. Sed sed consectetur augue.
<code><? echo "this is on a single line"; ?></code>
<code>
<?php
echo "this is a multiline code block";
?>
</code>
Maecenas eleifend egestas nisi ac fringilla. Integer lacinia posuere diam vitae aliquet. Donec augue diam, feugiat nec rhoncus id, lacinia sit amet libero. Nam eu commodo mauris.
[/text]
After
[text]<p>Aliquam erat volutpat. Morbi at justo lacus. Sed id dui felis, in fringilla urna. Aliquam erat volutpat. Maecenas nec diam ullamcorper nisi scelerisque vehicula. Sed sed consectetur augue. </p>
<code><? echo "this is on a single line"; ?></code>
<code>
<?php
echo "this is a multiline code block";
?>
</code>
<p>Maecenas eleifend egestas nisi ac fringilla. Integer lacinia posuere diam vitae aliquet. Donec augue diam, feugiat nec rhoncus id, lacinia sit amet libero. Nam eu commodo mauris. </p>
[/text]
I am trying to have a php function that takes the article and wraps only the paragraphs in a <p> tag.
The problem I am having is that I cannot make my regular expression work to only recognize the true paragraphs compared to code examples...
An example of before and after is below
Before:
[text]Aliquam erat volutpat. Morbi at justo lacus. Sed id dui felis, in fringilla urna. Aliquam erat volutpat. Maecenas nec diam ullamcorper nisi scelerisque vehicula. Sed sed consectetur augue.
<code><? echo "this is on a single line"; ?></code>
<code>
<?php
echo "this is a multiline code block";
?>
</code>
Maecenas eleifend egestas nisi ac fringilla. Integer lacinia posuere diam vitae aliquet. Donec augue diam, feugiat nec rhoncus id, lacinia sit amet libero. Nam eu commodo mauris.
[/text]
After
[text]<p>Aliquam erat volutpat. Morbi at justo lacus. Sed id dui felis, in fringilla urna. Aliquam erat volutpat. Maecenas nec diam ullamcorper nisi scelerisque vehicula. Sed sed consectetur augue. </p>
<code><? echo "this is on a single line"; ?></code>
<code>
<?php
echo "this is a multiline code block";
?>
</code>
<p>Maecenas eleifend egestas nisi ac fringilla. Integer lacinia posuere diam vitae aliquet. Donec augue diam, feugiat nec rhoncus id, lacinia sit amet libero. Nam eu commodo mauris. </p>
[/text]