Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

php - HTML safe wrapping of long lines

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 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 ! 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)(
)*/","$1
",$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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...