Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created November 16, 2013 22:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/7506523 to your computer and use it in GitHub Desktop.
Save xeoncross/7506523 to your computer and use it in GitHub Desktop.
Try to convert HTML, unicode, and other smart quotes to plain quotes. Fork this gist to add support for more types.
<?php
function convert_smart_quotes($string)
{
$quotes = array(
"\xC2\xAB" => '"', // « (U+00AB) in UTF-8
"\xC2\xBB" => '"', // » (U+00BB) in UTF-8
"\xE2\x80\x98" => "'", // ‘ (U+2018) in UTF-8
"\xE2\x80\x99" => "'", // ’ (U+2019) in UTF-8
"\xE2\x80\x9A" => "'", // ‚ (U+201A) in UTF-8
"\xE2\x80\x9B" => "'", // ‛ (U+201B) in UTF-8
"\xE2\x80\x9C" => '"', // “ (U+201C) in UTF-8
"\xE2\x80\x9D" => '"', // ” (U+201D) in UTF-8
"\xE2\x80\x9E" => '"', // „ (U+201E) in UTF-8
"\xE2\x80\x9F" => '"', // ‟ (U+201F) in UTF-8
"\xE2\x80\xB9" => "'", // ‹ (U+2039) in UTF-8
"\xE2\x80\xBA" => "'", // › (U+203A) in UTF-8
);
$string = strtr($string, $quotes);
// Version 2
$search = array(
chr(145),
chr(146),
chr(147),
chr(148),
chr(151)
);
$replace = array("'","'",'"','"',' - ');
$string = str_replace($search, $replace, $string);
// Version 3
$string = str_replace(
array('&#8216;','&#8217;','&#8220;','&#8221;'),
array("'", "'", '"', '"'),
$string
);
// Version 4
$search = array(
'&lsquo;',
'&rsquo;',
'&ldquo;',
'&rdquo;',
'&mdash;',
'&ndash;',
);
$replace = array("'","'",'"','"',' - ', '-');
$string = str_replace($search, $replace, $string);
return $string;
}
@Erenor
Copy link

Erenor commented Feb 7, 2023

A small question: what about duplicating the special codes to use both the uppercase and the lowercase versions? Like "\xE2\x80\x98" and "\xe2\x80\x98". Also, single quotes around these strings seems to work better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment