Link to home
Start Free TrialLog in
Avatar of mcorsi62

asked on

PHP - Email - Microsoft 'smart' quotes

I moved a bunch of php code over to a new server. Part of the code was responsible for gerating a daily email. The editor uses Microsoft Word. On the old server, the code ran fine (PHP 4.x; Apache 1.4) however on the new server (PHP 5.x; Apache 2.x) the code looks fine all the way till the reception of the email. The email displays gibberish in place of the MS 'smart' quotes.

So - I decided to just get rid of the smart quotes before sending. The code I need to clean up looks like this if you open it in VI :

 This is a double quote open  -     â<80><9c>   and   a  close   â<80><9d>^M
This is a single quote open  --  Ã¢<80><98>    and   a close  --    Ã¢<80><99>  

I found a few lines of code on the net to replace these stupid characters. it is:

$chrs = array (chr(150), chr(147), chr(148), chr(146));
$repl = array ("-", "\"", "\"", "'");
$HTMLcontent = str_replace($chrs, $repl, $HTMLcontent);

However, it does not recognize and replace the smart quotes so I am still left with gibberish in the email. The gibberish looks like this:

This is a double quote open - â¬S and a close ⬝
This is a single quote open -- â¬Ü and a close -- â¬"

Any suggestions on how to get this problem resolved?

Avatar of Isisagate

Try this one...

  $find[] = 'â¬S';  // left side double smart quote
  $find[] = '⬝';  // right side double smart quote
  $find[] = 'â¬Ü';  // left side single smart quote
  $find[] = 'â¬"';  // right side single smart quote
  $find[] = '⬦';  // elipsis
  $find[] = 'â¬';  // em dash
  $find[] = 'â¬';  // en dash

  $replace[] = '"';
  $replace[] = '"';
  $replace[] = "'";
  $replace[] = "'";
  $replace[] = "...";
  $replace[] = "-";
  $replace[] = "-";

  $text = str_replace($find, $replace, $text);

it's from the PHP manual
Avatar of mcorsi62


Sorry - but this doesn't work.

I have done an octal dump to give you an example of what I need to replace:

0000000   T   h   i   s       i   s       a       t   e   s   t       o
0000020   f     342 200 234   q   u   o   t   e   s 342 200 235   .  \r
0000040  \n  \r  \n   A   n   d       t   h   i   s       i   s       a
0000060       t   e   s   t       o   f     342 200 230   a   p   o   s
0000100   t   r   o   p   h   e   s 342 200 231   .   W   i   t   h    


$test = str_replace(chr(342), "\"", $text);

has no impact so the php script just doesn't seem to want to 'see' these characters for some reason.

This was the final solution guys - the chr() function needed hexidecimal values:

function convert_smart_quotes($string) {    
$search = array(chr(0xe2) . chr(0x80) . chr(0x98),chr(0xe2) . chr(0x80) . chr(0x99),chr(0xe2) . chr(0x80) . chr(0x9c),chr(0xe2) . chr(0x80) . chr(0x9d),chr(0xe2) . chr(0x80) . chr(0x93),chr(0xe2) . chr(0x80) . chr(0x94));
$replace = array('&lsquo;','&rsquo;','&ldquo;','&rdquo;','&ndash;','&mdash;');
return str_replace($search, $replace, $string);

Credit for this one goes to 'Mark' posting on
Avatar of Computer101
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial