简体   繁体   English

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

[英]Textarea input - how to deal with paragraphs and headings?

I currently use the following expression which I use to put paragraph tags around textarea input before storing it in a MySQL database. 我目前使用以下表达式,在将其存储在MySQL数据库中之前,使用该表达式在textarea输入周围放置段落标签。

$inputText = str_replace('<p></p>', '', '<p>' . preg_replace('#([\r\n]\s*?[\r\n]){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. 附带说明一下,我希望能够输入独立的超链接“ a”标签,并且仍然像以前一样用段落标签将它们包围起来。

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. 我认为,在将详细信息输入数据库以删除不需要的段落标记后,手动编辑这些详细信息可能会更容易。

I use this function from wordpress, wraps p's around paragraphs nicely as well as line breaks whilst preserving HTML: 我使用了来自wordpress的此函数,在保留HTML的同时,将p很好地环绕了段落以及换行符:

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

You can use the strip_tags function like this: 您可以像这样使用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>');
?>

It should work out. 它应该解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM