html和php怎么合并,PHP:通过合并换行符并正确删除空格来清理HTML

我正在使用所见即所得的编辑器,并且有一堆处理脏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

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("/(?:

\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是的,无论用户在哪里放置多个换行符,我都想将它们合并为一个换行符。问题是换行符的'结构'是相当不可预知的,请参阅上面的示例。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值