整理修复字符串功能移除末尾的换行符(\n或\r)。

4
$HTMLcodeOutput = tidy_repair_string($HTMLcode, array('show-body-only' => true, 'indent' => true));

将删除行末的$HTMLcode。 例如:

that's in the main box



##What does it do ?

it does everything



##Output:

将输出

that's in the main box ##What does it do ? it does everything
##Output: I will giv ea a grat output

如果这个字符串被输出到一个 <pre> 标签中,那么这就成了一个问题。因此,\n 或 \r 字符是重要的。 我查看了文档,但没有找到明显的解决方案。 有没有办法保留这些换行字符? (在使用整洁字符串修复之前添加 <br> 标签是一个坏主意,因为它会将 <pre> 标签作为字符串(已解码)显示在浏览器中,而不是作为标签。)
1个回答

3
如果我理解正确,您正在尝试以人类可读的方式格式化HTML,并且不想使用"pre"标签来强制Tidy保留空格,因为您将在输出到浏览器之前通过htmlspecialchars运行Tidy的输出。
那么,您为什么不在清理后删除这些"pre"标签呢?它将看起来像这样:
$config = array('show-body-only' => true, 'indent' => true);
$HTMLcodeOutput = tidy_repair_string('<pre>'.$HTMLcode.'</pre>', $config);
$HTMLcodeOutput = substr($HTMLcodeOutput, 5, -6);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接