简体   繁体   中英

Reading special characters from .txt file - PHP

I'm trying to output the content of a .txt file on a webpage using php/AJAX, which works fine for .txt files with no special characters in it. But when the .txt file contains characters such as accents on letters - è, for example, the AJAX request for the contents of the file returns null.

I think the problem originates from copying and pasting the content of the .txt file from a word document, as per a test I ran.

If you copy, for example, an apostrophe from a word document and paste it into a .txt via notepad, it appears differently to how it would if the apostrophe was typed directly into the notepad .txt file.

My client currently uploads .txt files to a certain folder on his server, and creates his content by copying and pasting from word. Is there a way around this, so that these 'copied and pasted' special characters can be read?

Example of formatting difference:

copied from word - Dave's

typed directly - Dave's

You see the difference in the apostrophe?

Can do htmlspecialchars() on the variable that contains the text file before echoing?

That should convert any special characters into HTML entities and should be view able on a webpage.

ok))

  1. do not use notepad, ever
  2. save all the files with the utf8 encoding

that's it, problem solved

These are called Smart Quotes. You can change them in Word options by following the guide located here . This would be the best option as it defines your intended output at the source. If this is not an option, ie there are thousands of Word files at a time, then post-processing is the way to go. This can be accomplished by looking up the HTML Entity for the characters and using a find-and-replace function. I suggest using PHP to open the text file using the str_replace() function to find and replace all occurrences of unwanted/undisplayable characters, which themselves could be defined in array or the like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM