长线的HTML安全包装

长线的HTML安全包装

本文介绍了长线的HTML安全包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发送带有长行文本的HTML电子邮件时遇到问题。网站上使用的所见即所得编辑器(FCKEditor 2.5)不断删除某些浏览器(包括IE和Chrome)上的所有 \\\
字符。结果是一封电子邮件带有一大堆文字。如果不是通过插入的电子邮件客户端包含超过998个字符的线条,这不会是一个问题! \\\
里面。当然,这几乎总是在最不幸的地方,打破了HTML标签,并在内容本身看起来很讨厌。



我的初始解决方案是添加一个换行符每个HTML标签或每900到990个字符。这是正则表达式,我结束了:

  return preg_replace(/(< \ / [^ \& ] +> |< [^ \>] + \ /> |> [^<] {900990} \s)(\\\
)* /, $ 1\\\
,$海峡);

但是,当有行不包含任何标签的时候,空格匹配部分是从未触发过。但是,如果我从头开始删除> ,那么它会开始破坏标签。



有没有更好的方法正则表达式可以这样做,还是可以修正正则表达式?



编辑: 1000个字符行长度限制在。

解决方案

在我的评论之后,我发布了这个,因为我已经能够进行测试。



完成的工作很好,比任何正则表达式解决方案都好。

  $ content =< html> ......< / html>; 
$ oTidy = new tidy();
$ content = $ oTidy-> repairString($ content,
array(show-errors=> 0,show-warnings=> false),
utf8
);

根据需要调整Charset参数(3rd)。



干净的选项不需要这个,我的评论错了。


I'm having problems sending HTML emails with long lines of text. The WYSIWYG editor (FCKEditor 2.5) used on the site keeps removing all the \n characters on certain browsers, including IE and Chrome. The result is an email with a single huge line of text. This wouldn't be a problem if it wasn't for email clients that wrap lines of over 998 characters by inserting ! \n in it. Of course, these almost always end up in the most unfortunate places, breaking HTML tags and looking nasty in the content itself.

My initial solution was to add a line feed after every HTML tag or every 900 to 990 characters. This is the regex I ended up with:

 return preg_replace("/(<\/[^\>]+>|<[^\>]+\/>|>[^<]{900,990}\s)(\n)*/","$1\n",$str);

However, when there are lines that don't contain any tags at all, the whitespace matching part is never triggered. But if I remove the > from it's beginning, it starts breaking tags.

Is there a better way than regex to do this, or can this regex be healed?

EDIT: The 1000 character line length limit is defined in RFC 821.

解决方案

Following my comment, I'm posting this as I have been able to run a test.

tidy::repairString shoud do the job just fine, better than any regex solution.

$content = "<html>......</html>";
$oTidy = new tidy();
$content = $oTidy->repairString($content,
    array("show-errors" => 0, "show-warnings" => false),
    "utf8"
);

Adapt the Charset parameter (3rd) to your needs.

The clean option is unneeded for this, I was wrong in my comment.

这篇关于长线的HTML安全包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:46