这个问题可以通过自定义 nl2br()
函数来部分地(如果不是完全地)解决:
function nl2br_special($string){
// Step 1: Add <br /> tags for each line-break
$string = nl2br($string);
// Step 2: Remove the actual line-breaks
$string = str_replace("\n", "", $string);
$string = str_replace("\r", "", $string);
// Step 3: Restore the line-breaks that are inside <pre></pre> tags
if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
foreach($match as $a){
foreach($a as $b){
$string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", PHP_EOL, $b)."</pre>", $string);
}
}
}
// Step 4: Removes extra <br /> tags
// Before <pre> tags
$string = str_replace("<br /><br /><br /><pre>", '<br /><br /><pre>', $string);
// After </pre> tags
$string = str_replace("</pre><br /><br />", '</pre><br />', $string);
// Arround <ul></ul> tags
$string = str_replace("<br /><br /><ul>", '<br /><ul>', $string);
$string = str_replace("</ul><br /><br />", '</ul><br />', $string);
// Inside <ul> </ul> tags
$string = str_replace("<ul><br />", '<ul>', $string);
$string = str_replace("<br /></ul>", '</ul>', $string);
// Arround <ol></ol> tags
$string = str_replace("<br /><br /><ol>", '<br /><ol>', $string);
$string = str_replace("</ol><br /><br />", '</ol><br />', $string);
// Inside <ol> </ol> tags
$string = str_replace("<ol><br />", '<ol>', $string);
$string = str_replace("<br /></ol>", '</ol>', $string);
// Arround <li></li> tags
$string = str_replace("<br /><li>", '<li>', $string);
$string = str_replace("</li><br />", '</li>', $string);
return $string;
}
在进行HTML净化之前,必须将此应用于内容。除非你知道自己在做什么,否则不要重新处理已净化的内容。
请注意,因为每个换行符和双重换行符已经保留,所以不应使用HTML Purifier的AutoFormat.AutoParagraph功能:
$string = nl2br_special($string);
$purifier_config = HTMLPurifier_Config::createDefault();
$purifier_config->set('HTML.Allowed', 'p,ul,ol,li,strong,b,em,i,u,a[href],code,pre,blockquote,cite,img[src|alt],br,hr,h3,h4');
$purifier = new HTMLPurifier($purifier_config);
$string = $purifier->purify($string);
就是这样!
此外,允许基本的HTML标签最初是为了
通过不添加其他标记语法来提高用户体验,因此您可能希望允许用户发布代码,特别是HTML代码,这些代码将不会被HTML净化器解释/删除。
HTML净化器目前允许发布代码,但需要复杂的CDATA标记:
<![CDATA[
Place code here
]]>
很难记住和写下来。为了尽可能简化用户体验,我认为最好允许用户通过嵌入简单的<code>
(用于内联代码)和<pre>
(用于代码块)标签来添加代码。以下是如何实现:
function custom_code_tag_callback($code) {
return '<code>'.trim(htmlspecialchars($code[1])).'</code>';
}
function custom_pre_tag_callback($code) {
return '<pre><code>'.trim(htmlspecialchars($code[1])).'</code></pre>';
}
// Don't require HTMLPurifier's CDATA enclosing, instead allow simple <code> or <pre> tags
$string = preg_replace_callback("/\<code\>(.*?)\<\/code\>/is", 'custom_code_tag_callback', $string);
$string = preg_replace_callback("/\<pre\>(.*?)\<\/pre\>/is", 'custom_pre_tag_callback', $string);
请注意,与nl2br处理类似,必须在内容被HTML净化之前完成。此外,请记住,如果用户在自己发布的代码中放置了
<code>
或
<pre>
标签,则会关闭包含其代码的父
<code>
或
<pre>
标签。这无法解决,并且适用于原始CDATA标记或任何标记,甚至是在StackOverflow上使用的标记(例如,在代码示例中使用`符号将关闭代码标记)。
最后,为了获得良好的用户体验,我们可能希望自动化其他一些事情,例如我们希望链接可以点击。幸运的是,这可以通过HTML Purifier的
AutoFormat.Linkify
功能来实现。
以下是包括所有内容的最终代码,以获取终极设置:
// === Declare functions ===
function nl2br_special($string){
// Step 1: Add <br /> tags for each line-break
$string = nl2br($string);
// Step 2: Remove the actual line-breaks
$string = str_replace("\n", "", $string);
$string = str_replace("\r", "", $string);
// Step 3: Restore the line-breaks that are inside <pre></pre> tags
if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
foreach($match as $a){
foreach($a as $b){
$string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", PHP_EOL, $b)."</pre>", $string);
}
}
}
// Step 4: Removes extra <br /> tags
// Before <pre> tags
$string = str_replace("<br /><br /><br /><pre>", '<br /><br /><pre>', $string);
// After </pre> tags
$string = str_replace("</pre><br /><br />", '</pre><br />', $string);
// Arround <ul></ul> tags
$string = str_replace("<br /><br /><ul>", '<br /><ul>', $string);
$string = str_replace("</ul><br /><br />", '</ul><br />', $string);
// Inside <ul> </ul> tags
$string = str_replace("<ul><br />", '<ul>', $string);
$string = str_replace("<br /></ul>", '</ul>', $string);
// Arround <ol></ol> tags
$string = str_replace("<br /><br /><ol>", '<br /><ol>', $string);
$string = str_replace("</ol><br /><br />", '</ol><br />', $string);
// Inside <ol> </ol> tags
$string = str_replace("<ol><br />", '<ol>', $string);
$string = str_replace("<br /></ol>", '</ol>', $string);
// Arround <li></li> tags
$string = str_replace("<br /><li>", '<li>', $string);
$string = str_replace("</li><br />", '</li>', $string);
return $string;
}
function custom_code_tag_callback($code) {
return '<code>'.trim(htmlspecialchars($code[1])).'</code>';
}
function custom_pre_tag_callback($code) {
return '<pre><code>'.trim(htmlspecialchars($code[1])).'</code></pre>';
}
// === Process user's input ===
// Process line-breaks
$string = nl2br_special($string);
// Allow simple <code> or <pre> tags for posting code
$string = preg_replace_callback("/\<code\>(.*?)\<\/code\>/is", 'custom_code_tag_callback', $string);
$string = preg_replace_callback("/\<pre\>(.*?)\<\/pre\>/is", 'custom_pre_tag_callback', $string);
// Initiate HTML Purifier config
$purifier_config = HTMLPurifier_Config::createDefault();
$purifier_config->set('HTML.Allowed', 'p,ul,ol,li,strong,b,em,i,u,a[href],code,pre,blockquote,cite,img[src|alt],br,hr,h3,h4');
$purifier_config->set('AutoFormat.Linkify', true); // Make links clickable
//$purifier_config->set('HTML.TargetBlank', true); // Uncomment if you want links to open new tabs
//$purifier_config->set('AutoFormat.AutoParagraph', true); // Leave this commented as it conflicts with nl2br
// Initiate HTML Purifier
$purifier = new HTMLPurifier($purifier_config);
// Purify the content!
$string = $purifier->purify($string);
干杯!
nl2br
。 在您的情况下,您已经有了 HTML。 为什么您的 HTML 没有正确地包含用于换行的<br>
? - deceze<br>
标签。也许他正在使用HTML中的换行符,因为它们旨在使标记更易读,而不实际引入文本中的换行符。就我而言,你不能两者兼得。 :) 你真的需要解析HTML并仅在特定文本节点上应用nl2br
,排除<pre>
元素。 - deceze<br>
标签,您需要解析HTML以仅在某些元素上执行此操作。但是,在对HTML进行清理之前,您必须这样做,这意味着您可能无法正确解析HTML。这是一个非常棘手的问题。我了解您尝试做什么,但它很棘手且容易出错的原因是Markdown和其他类似工具首次出现的原因。而SO并不是它有效的好例子,因为它并不起作用。 - decezenl2br
。如果Purifier默认破坏了输入中固有的换行符,因此您无法在之后执行第二步,则需要自定义Purifier以以不同的方式运行和/或将nl2br
直接卷入其处理中。您调查过这种可能性吗?我现在无法用代码给您提供解决方案。 - deceze