我正在使用所见即所得的编辑器,并且有一堆处理脏HTML的正则表达式。原因:我的用户经常打的过于频繁输入键,产生许多新的冗余线路,如:PHP:通过合并换行符并正确删除空格来清理HTML
...
和更多的品种包括丁p, 和br
这是我尝试目前打这样的投入,试图合并许多连续换行到1,使用许多不同的正则表达式:
// merge empty p tags into one
// http://stackoverflow.com/q/16809336/1066234
$content = preg_replace('/((
\s*) (\s*))+/im', "
// remove sceditor's:
\n
\n
// 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("/(?:
\s*(
\s*)+\s*\s*)+$/", "", $content);
// remove sceditor's double: http://http://
$content = str_replace('http://http://', 'http://', $content);
// remove spaces from end of string ( )
$content = preg_replace('/( )+$/', '', $content);
// remove also
$content = preg_replace('/(
)+$/', '', $content);
// remove line breaks from end of string - $ is end of line, +$ is end of line including \n
// html with
$content = preg_replace('/(
)+$/', '', $content);
$content = preg_replace('/(
)+$/', '', $content);
// remove line breaks from beginning of string
$content = preg_replace('/^(
)+/', '', $content);
我寻找新的解决方案。有什么HTML解析器,我可以告诉合并换行符和空格?或者也许有人有另一种解决这个问题的方法。
上述的正则表达式解决方案似乎不够合适,因为我的用户的换行符“尝试”的新组合滑过。
+0
我想在所见即所得的水平上解决这个问题。正则表达式1不需要'm'修饰符,你可能想在那里使用's'修饰符。 –
+0
我理解你正确吗?你想删除每一个空行换行符? –
+0
@ AMartinNo1是的,无论用户在哪里放置多个换行符,我都想将它们合并为一个换行符。问题是换行符的'结构'是相当不可预知的,请参阅上面的示例。 –