I need to replace Microsoft Word's version of single and double quotations marks (“ ” ‘ ’
) with regular quotes (' and ") due to an encoding issue in my application. I do not need them to be HTML entities and I cannot change my database schema.
I have two options: to use either a regular expression or an associated array.
Is there a better way to do this?
Your Microsoft-encoded quotes are the probably the typographic quotation marks. You can simply replace them with
str_replace
if you know the encoding of the string in that you want to replace them.Here’s an example for UTF-8 but using a single mapping array with
strtr
:If you’re need another encoding, you can use
mb_convert_encoding
to convert the keys.If like me you arrive here with an enormous range of broken ASCII / Microsoft Word characters that are doing weird things to your CMS or RTE and iconv isn't working, then this mad function might just be for you.
Make sure your encoding is UTF-8 when you save this function to a file.
I have found an answer to this question. You need just one line of code using
iconv()
function in php:Every single one of the previous answers except for Gumbo's will mangle Unicode strings:
Results in:
The iconv:
Results in:
You can change it to
//IGNORE
, which will remove the characters, but not translate them.This is the best way to replace Microsoft quotes encoded in CP1252. If they are in Unicode and you need to replace them, use Gumbo's answer:
Taken from this answer, with some modifications. If you want to control over what you find/replace, use that function.
We used the following. It deals with a few more special characters.
Considering you only want to replace a few specific and well identified characters, I would go for
str_replace
with an array: you obviously don't need the heavy artillery regex will bring you ;-)And if you encounter some other special characters (damn copy-paste from Microsoft Word...), you can just add them to that array whenever is necessary / whenever they are identified.
The best answer I can give to your comment is probably this link: Convert Smart Quotes with PHP
And the associated code (quoting that page):
(I don't have Microsoft Word on this computer, so I can't test by myself)
I don't remember exactly what we used at work (I was not the one having to deal with that kind of input), but it was the same kind of stuff...