【问题标题】:Textarea input - how to deal with paragraphs and headings?Textarea 输入 - 如何处理段落和标题?
【发布时间】:2010-10-15 11:46:35
【问题描述】:

我目前使用以下表达式,在将文本区域输入存储到 MySQL 数据库之前,我使用它在文本区域输入周围放置段落标签。

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

这很好用,除非我想使用标题标签。然后这些被不需要的段落标签包围:

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

虽然这按预期显示,但从验证的角度来看并不是很好。

任何人都可以提出改进的表达式和/或方法来捕获标题标签并仅将段落标签应用于实际段落吗?或者,我可以在我当前使用的表达式之前将其应用于我的输入以产生相同的预期效果。

作为旁注,我希望能够输入独立的超链接“a”标签,并且仍然像以前一样用段落标签包围它们。

我考虑过,在将细节输入数据库后手动编辑细节以删除不需要的段落标签可能会更容易。

【问题讨论】:

    标签: php input format textarea paragraph


    【解决方案1】:

    我使用 wordpress 中的这个功能,很好地将 p 包裹在段落周围以及换行符,同时保留 HTML:

    function wpautop($pee, $br = 1) {
        $pee = $pee . "\n"; // just to make things a little easier, pad the end
        $pee = preg_replace('|<br />\s*<br />|', "\n\n", $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 . '[^>]*>)!', "\n$1", $pee);
        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
        $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
        $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
        $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $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*\n|', "<br />\n", $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( "|\n</p>$|", '</p>', $pee );
        return $pee;
    }
    

    【讨论】:

    • 我不确定这个功能是如何工作的,但它确实可以满足我的需求。想知道您是否从 WordPress 版本中完全改变了这一点?
    • 不,它来自一个稍微旧的版本,但最新版本的功能几乎相同,并且做同样的工作
    【解决方案2】:

    你可以像这样使用 strip_tags 函数:

    <?php
    $text = '<p><h3>Test Header</h3></p>';
    echo strip_tags($text);
    echo "\n";
    
    // Allow <p> and <h3>
    echo strip_tags($text, '<p><h3>');
    ?>
    

    应该会成功的。

    【讨论】:

    • 这种方法固然有用,但不如从WordPress偷来的功能全面。谢谢!
    猜你喜欢
    • 2011-02-06
    • 1970-01-01
    • 2016-06-13
    • 2013-06-13
    • 2012-09-02
    • 2019-04-08
    • 2023-04-02
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多