【问题标题】:PHP: Clean HTML by merging line breaks and removing whitespaces properlyPHP:通过合并换行符和正确删除空格来清理 HTML
【发布时间】:2023-03-09 04:39:01
【问题描述】:

我正在使用所见即所得的编辑器,并且有一堆处理脏 HTML 的正则表达式。原因:我的用户经常按回车键并产生许多多余的新行,例如:

  • <br><br><br> ...
  • <p> <br /> </p>
  • <p> <br /><br /> </p>
  • <p> <br /> </p>
  • <p>   <br /> </p>
  • <p>   <br /> </p>
  • 还有更多品种,包括p br

这就是我目前尝试对抗此类输入的方式,尝试使用许多不同的正则表达式将许多连续的换行符合并为 1:

// merge empty p tags into one
// http://stackoverflow.com/q/16809336/1066234
$content = preg_replace('/((<p\s*\/?>\s*)&nbsp;(<\/p\s*\/?>\s*))+/im', "<p>&nbsp;</p>\n", $content);

// remove sceditor's: <p>\n<br>\n</p> from end of string
// http://stackoverflow.com/questions/25269584/how-to-replace-pbr-p-from-end-of-string-that-contain-whitespaces-linebrea
// \s* matches any number of whitespace characters (" ", \t, \n, etc)
// (?:...)+ matches one or more (without capturing the group)
// $ forces match to only be made at the end of the string
$content = preg_replace("/(?:<p>\s*(<br>\s*)+\s*<\/p>\s*)+$/", "", $content);

// remove sceditor's double: http://http://
$content = str_replace('http://http://', 'http://', $content);

// remove spaces from end of string (&nbsp;)
$content = preg_replace('/(&nbsp;)+$/', '', $content);

// remove also <p><br></p> from end of string
$content = preg_replace('/(<p><br><\/p>)+$/', '', $content);

// remove line breaks from end of string - $ is end of line, +$ is end of line including \n
// html with <p>&nbsp;</p>
$content = preg_replace('/(<p>&nbsp;<\/p>)+$/', '', $content);
$content = preg_replace('/(<br>)+$/', '', $content);

// remove line breaks from beginning of string
$content = preg_replace('/^(<p>&nbsp;<\/p>)+/', '', $content);

我正在寻找新的解决方案。是否有任何 HTML 解析器可以告诉我合并换行符和空格?或者也许有人对这个问题有另一种方法。

上面的正则表达式解决方案似乎不够合适,因为我的用户对换行“尝试”的新组合漏掉了。

【问题讨论】:

  • 我会尝试在所见即所得级别解决问题。正则表达式 1 不需要 m 修饰符,您可能需要 s 修饰符..
  • 我的理解是否正确?您想删除每个空换行符吗?
  • @AMartinNo1 是的,在用户放置多个换行符的任何地方,我都想将它们合并为 1 个换行符。问题是换行符的“结构”非常不可预测,请参见上面的示例。
  • 我明白了。如果有人出于某种原因想要有多个换行符怎么办?
  • 根据我几年来的经验,我可以告诉大多数用户认为换行有助于问题的视觉印象,并在问题末尾添加大约 5 - 10 个换行符,从而产生不必要的白色空间。但你是对的,我们可以允许 2 个换行符。上面的问题仍然没有解决:)

标签: php html


【解决方案1】:

我开发了以下 sn-p 删除重复的br-Tags。

<?php
$content = "<h1>Hello World</h1><p>Test\r\n<br>\r\n<br >\r\n<br    >\r\n<br/>Test\r\n<br />\r\n<br    /></p>";

echo "<code>{$content}</code><hr>\r\n\r\n\r\n\r\n";

$contentStripped = preg_replace('/(<br {0,}\/{0,1}>(\\r|\\n){0,}){2,}/', '<br class="reduced" />', $content);
echo "<code>{$contentStripped}</code>\r\n\r\n\r\n\r\n";


您可能需要添加更多测试用例。

【讨论】:

    【解决方案2】:

    您可以使用 nl2br(strip_tags($content)) 代替上面的长代码。

    【讨论】:

    • strip_tags 的问题在于它删除了每个br-tag,但他不想删除每个br-tag。此外,他还必须将几乎所有 html 标签都添加到允许列表中,以便删除不需要的标签。
    • 不,我里面还有一堆其他的 HTML 必须保存下来。使用 strip_tags 将删除所有标签。这种解决方案是不可接受的。
    • strip_tags 不会删除所有 htmls 标签,它确实允许排除某些 html 标签的第二个参数。字符串 strip_tags ( 字符串 $str [, 字符串 $allowable_tags ] ) php.net/manual/en/function.strip-tags.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2015-07-29
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多