[英]PHP: How to keep line-breaks using nl2br() with HTML Purifier?
问题:使用HTML Purifier处理用户输入的内容时,换行符不会转换为<br />
标记。
请考虑以下用户输入的内容:
Lorem ipsum dolor sit amet.
This is another line.
<pre>
.my-css-class {
color: blue;
}
</pre>
Lorem ipsum:
<ul>
<li>Lorem</li>
<li>Ipsum</li>
<li>Dolor</li>
</ul>
Dolor sit amet,
MyName
使用HTML Purifier处理时,以上内容将更改为以下内容:
Lorem ipsum dolor坐着。 这是另一条线。
.my-css-class { color: blue; }
Lorem ipsum:
Dolor坐下来,MyName
- LOREM
- 存有
- 悲
正如您所看到的,用户在一条单独的行上的“ MyName ”与前一行一起显示。
当然,使用PHP nl2br()
函数。 但是,无论我们在净化内容之前还是之后使用它都会出现新问题。
以下是在HTML Purifier之前使用nl2br()时的示例:
Lorem ipsum dolor坐着。
这是另一条线。.my-css-class { color: blue; }
Lorem ipsum:
- LOREM
- 存有
- 悲
Dolor坐着,
我的名字
会发生的事情是nl2br()为每个换行符添加了<br />
,因此即使正在处理<pre>
块中的那些换行符,也会在每个<li>
标记之后添加换行符。
我尝试了一个自定义的nl2br()函数 ,用<br />
标记替换换行符,然后从<pre>
块中删除所有<br />
标记。 它工作得很好,但问题仍然存在于<li>
项目中。
尝试了同样的做法<ul>
块也将删除所有<br />
从标签<li>
的孩子,除非我们将使用更复杂的正则表达式来删除<br />
是内部标记<ul>
元素,但外面<li>
元素。 但那么<li>
项目中的嵌套<ul>
呢? 要处理所有这些情况,我们必须拥有更复杂的正则表达式!
我已经看过的其他资源:
使用自定义nl2br()
函数可以部分解决此问题(如果不是完全解决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-Purified 之前将其应用于内容。 除非您知道自己在做什么,否则永远不要重新处理纯化的内容。
请注意,因为已经保留了每个换行符和双换行符,所以不应使用HTML Purifier的AutoFormat.AutoParagraph
功能:
// Process line-breaks
$string = nl2br_special($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.AutoParagraph', true); // Make sure to NOT use this
// Initiate HTML Purifier
$purifier = new HTMLPurifier($purifier_config);
// Purify the content!
$string = $purifier->purify($string);
而已!
此外,由于允许基本HTML标记最初旨在通过不添加其他标记语法来改善用户体验 ,因此您可能希望允许用户发布代码,尤其是HTML代码,这些代码不会被HTML Purifier解释/删除。
HTML Purifier目前允许发布代码,但需要复杂的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 Purified之前完成。 另外,请记住,如果用户将<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);
干杯!
也许这会有所帮助。
function custom_nl2br($html) {
$pattern = "/<ul>(.*?)<\/ul>/s";
preg_match($pattern, $html, $matches);
$html = nl2br(str_replace($matches[0], '[placeholder]', $html));
$html = str_replace('[placeholder]',$matches[0], $html);
return $html;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.