How can PHP detect paragraphs and add <p> tags for HTML output

1.6k Views Asked by At

How do I make PHP detect paragraphs and add and parse

tags for HTML output?

I'm making a blog with bbcode support but I'm still missing the

tags in the HTML output of the blog posts. How can I make PHP detect and add

tags on it's output?

I have currenly this code to parse just some basics bbcode and GeSHi code:

<?php
function code($match) {
    require_once './class/geshi/geshi.php';
    $geshi = new GeSHi($match[2], $match[1]);
    return $geshi->parse_code();
}
function bbcode($input) {

    $input  = strip_tags($input);
    $input  = htmlentities($input);

    $bbcodes = array(        
        "/\[b\](.*?)\[\/b\]/is" => "<b>$1</b>",
        "/\[u\](.*?)\[\/u\]/" => "<u>$1</u>",
        "/\[i\](.*?)\[\/i\]/" => "<i>$1</i>",
        "/\[d\](.*?)\[\/d\]/" => "<del>$1</del>",
        "/\[url=(.*?)\](.*?)\[\/url\]/" => "<a href='$1'>$2</a>"
    );
    $input  = preg_replace(array_keys($bbcodes), array_values($bbcodes), $input);

    //Check for code and add GeSHi:
    $input  = preg_replace_callback('~\[code=(.*?)\](.*?)\[\/code\]~is', 'code', $input);

    return $input;

}
?>

If the user submits in textarea the following example:

This is [b]bold and [i]italic[/i][/b].

This is some [u]PHP[/u] code:
[code=php]
echo 'Hello world!';
[/code]

That's all folk!

The HTML output is:

This is <b>bold and <i>italic</i></b>.

This is some <u>PHP</u> code:
<pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hello world!'</span><span style="color: #339933;">;</span>
&nbsp;</pre>

That's all folk!

...with no p tags. So how do I add this feature?

0

There are 0 best solutions below