0

I am attempting to use the solution found in this link to remove Microsoft smart quotes from a string before I export the data to CSV file. However, it doesn't seem to be working for me.

This is my code:

$Wp_NewsText = str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(151)),
    array("'", "'", "\"", "\"", "-"),
    $Wp_NewsText
);

But for some reason the download still contains the smart quotes. I thought it might be that, because a CSV needs double quotes as a delimiter, it was somehow reverting the quotes back to smart quotes, so I tried this:

$Wp_NewsText = str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(151)),
    array("'", "'", """, """, "‐"),
    $Wp_NewsText
);

But that didn't solve the problem. So I tried copy-pasting the actual characters in to the array:

$Wp_NewsText = str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(151), "“", "”"),
    array("'", "'", """, """, "‐", """, """),
    $Wp_NewsText
);

But alas, still no luck. Finally I've tried JSON encoding/decoding everything because apparently that's faster, but I was hoping it might have some other useful side-effect of making the code work.

$Wp_NewsText = json_decode(
    str_replace(
        array(chr(145), chr(146), chr(147), chr(148), chr(151), "“", "”"),
        array("'", "'", """, """, "‐", """, """),
        json_encode($Wp_NewsText)
    )
);

Nope.

The actual string I'm attempting to replace these characters in is as follows:

“There is significant intellectual property to be gleaned from these advanced automotive technology development programs; not least being the pursuit of silent drivetrains, where we’re focusing significant effort,” Setter said.

EDIT: If it matters, all of my code and text is UTF-8 encoded.

mpdc
  • 3,550
  • 5
  • 25
  • 48
  • I tested your 3rd example with the additional `"“", "”"` with my PHP 7.1.1 interpreter and it actually does what you want, it replaces the quotes with `"` like expected!? – xander Sep 28 '17 at 14:51
  • Here the result `"There is significant intellectual property to be gleaned from these advanced automotive technology development programs; not least being the pursuit of silent drivetrains, where we’re focusing significant effort," Setter said.`. – xander Sep 28 '17 at 14:52
  • Interesting... neither my code, nor http://phpfiddle.org/, worked for me. – mpdc Sep 28 '17 at 15:01
  • you're right phpfiddle.org seems not to work, but this is for me: https://ideone.com/8jPig4 seems to be an encoding problem if your local PHP version isnt working either, but the code seems to be working as you can see. – xander Sep 28 '17 at 15:07

0 Answers0