Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace Microsoft-encoded quotes in PHP

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?

like image 385
Misha M Avatar asked Aug 11 '09 18:08

Misha M


People also ask

How do you put quotation marks in PHP?

A single-quoted string only uses the escape sequences for a single quote or a backslash. Here are some common escape sequences for double-quoted strings: \" for a double quote. \\ for a backslash.

How do you encode a quote?

Encoding quotation marks (") is in practice only needed if the're inside an attribute, however for the HTML code to be correct (passing HTML validation), you should always encode quotation marks as " . Apostrophes (') don't need escaping in HTML. In XHTML they should be encoded as ' .


1 Answers

I have found an answer to this question. You need just one line of code using iconv() function in php:

// replace Microsoft Word version of single  and double quotations marks (“ ” ‘ ’) with  regular quotes (' and ") $output = iconv('UTF-8', 'ASCII//TRANSLIT', $input);      
like image 174
Justin Dominic Avatar answered Sep 19 '22 20:09

Justin Dominic