doq8211 2010-10-15 11:46
浏览 228
已采纳

Textarea输入 - 如何处理段落和标题?

I currently use the following expression which I use to put paragraph tags around textarea input before storing it in a MySQL database.

$inputText = str_replace('<p></p>', '', '<p>' . preg_replace('#([
]\s*?[
]){2,}#', '</p>$0<p>', $inputText) . '</p>');

This works well and good, except when I wish to use header tags. These are then surrounded by unwanted paragraph tags:

<p><h3>Test Header</h3></p>

While this displays as expected, it is not great from a validation point of view.

Can anyone suggest an improved expression and/or method to catch headers tags and only apply the paragraph tags to actual paragraphs? Or, an expression which I can apply to my input prior to the expression I'm currently using to produce the same desired effect.

As a side note, I would like to be able to enter stand-alone hyperlink 'a' tags and still have them surrounded with paragraph tags as before.

I have considered that it may just be easier to manually edit the details after they are entered into the database to remove the unwanted paragraph tags.

  • 写回答

2条回答 默认 最新

  • duanhuoyao7011 2010-10-15 11:56
    关注

    I use this function from wordpress, wraps p's around paragraphs nicely as well as line breaks whilst preserving HTML:

    function wpautop($pee, $br = 1) {
        $pee = $pee . "
    "; // just to make things a little easier, pad the end
        $pee = preg_replace('|<br />\s*<br />|', "
    
    ", $pee);
        // Space things out a little
        $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
        $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "
    $1", $pee);
        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1
    
    ", $pee);
        $pee = str_replace(array("
    ", ""), "
    ", $pee); // cross-platform newlines
        $pee = preg_replace("/
    
    +/", "
    
    ", $pee); // take care of duplicates
        $pee = preg_replace('/
    ?(.+?)(?:
    \s*
    |\z)/s', "<p>$1</p>
    ", $pee); // make paragraphs, including one at the end
        $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
        $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
        $pee = preg_replace( '|<p>|', "$1<p>", $pee );
        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
        $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
        $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
        $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
        if ($br) {
            $pee = preg_replace('|(?<!<br />)\s*
    |', "<br />
    ", $pee); // optionally make line breaks
        }
        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
        $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
        $pee = preg_replace( "|
    </p>$|", '</p>', $pee );
        return $pee;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错