PHP如何检测段落并为HTML输出添加<p>标签</p>

时间:2013-02-01 14:59:25

标签: php bbcode geshi paragraphs

如何让PHP检测段落并为HTML输出添加和解析

标记?

我正在制作一个支持bbcode的博客,但我仍然缺少博客帖子的HTML输出中的

标签。如何在其输出上进行PHP检测并添加

标记?

我当前有这个代码只解析一些基本的bbcode和GeSHi代码:

<?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;

}
?>

如果用户在textarea中提交以下示例:

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!

HTML输出是:

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!

...没有p标签。那么如何添加此功能呢?

0 个答案:

没有答案
相关问题