Converting a Word document into usable HTML in PHP

2020-02-10 08:17发布

I have a set of Word documents which I want to publish using a PHP tool I've written. I copy and paste the Word documents into a text box and then save them into MySQL using the PHP program. The problem I Have arises from all the non-standard characters that Word documents have, like curly quotes and ellipses ("..."). What I do at the moment is manually search and replace these kinds of things (and also foreign symbols such as e-acute) with either plain text or HTML entities (&eacute ; etc) Is there a function in PHP I can call that will take the output of a Word document and convert everything that should be entities into entities, and other symbols that don't display properly in Firefox into symbols that do display.

Thanks!

标签: php ms-word
5条回答
We Are One
2楼-- · 2020-02-10 08:26

htmlspecialchars() will get you a long way, but watch out because Word documents are messy.

查看更多
我命由我不由天
3楼-- · 2020-02-10 08:29

I think that all these answers miss one vital point. Windows itself uses a windows flavour of latin1, so if you paste some special characters in (like asymetrical quotes) into a form on a windows machine and that gets sent to a unix (or anything non-muckrosoft) box (be that to a database or whatever) some of the characters do not get matched to anything the unix system comprehends, hence the confused and garbled characters. What this means is that even if you have a UTF-8 database, and use htmlentities, some nasties are still going to get through because they are characters the OS doesn't recognise - they aren't even part of UTF-8 - the are microsoft-only inventions. I would love to know of a slick solution - what I do is manually blacklist the character codes of the microsoft-only chars I have encountered with an (also manual) list of UTF-8 characters, do a str_replace for all of these, and THEN you can do whatever you want with them - iconv, htmlentities, save straight into an utf8 database, it matters not anymore.

My grasp on this all is a little shaky - check out http://www.cs.tut.fi/~jkorpela/www/windows-chars.html for an excellent explanation which I have mutilated into short form above. - If someone has a better solution (surely there is one out there!) of how to PHPify what this article explains... I would love to hear it!

查看更多
神经病院院长
4楼-- · 2020-02-10 08:38

This has served me well in the past:

$str = mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8')
查看更多
够拽才男人
5楼-- · 2020-02-10 08:42

A better solution would be to ensure that your database is set-up to support UTF-8 characters. The additional characters available in the extended set should cover all the "non-standard" characters that you're talking about.

Otherwise, if you really must convert these characters into HTML entities, use htmlentities().

查看更多
对你真心纯属浪费
6楼-- · 2020-02-10 08:42

Here's a solution I cooked up for the problem with the non-portable windows character set. This replaces the offending almost-Latin-1 characters with their equivalent HTML entities.

$translation=array(
    // reference from http://www.cs.tut.fi/~jkorpela/www/windows-chars.html
    "\x82" => "‚",
    "\x83" => "ƒ",
    "\x84" => "„",
    "\x85" => "…",
    "\x86" => "†",
    "\x87" => "‡",
    "\x88" => "ˆ",
    "\x89" => "‰",
    "\x8a" => "Š",
    "\x8b" => "‹",
    "\x8c" => "Œ",
    "\x91" => "‘",
    "\x92" => "’",
    "\x93" => "“",
    "\x94" => "”",
    "\x95" => "•",
    "\x96" => "–",
    "\x97" => "—",
    "\x98" => "˜",
    "\x99" => "™",
    "\x9a" => "š",
    "\x9b" => "›",
    "\x9c" => "œ",
    "\x9f" => "Ÿ",
);    
return str_replace(array_keys($translation),array_values($translation),$input);

It Works For MeTM

查看更多
登录 后发表回答