سؤال

I am having a really hard time trying to fix a Clients Database records. I need to Find and Replace all Curly Quotes which look like this with Straight Quotes "

enter image description here

Attempt 1
I have tried to run this on my MySQL database with no luck.

update wp_posts set post_content = replace(post_content,'“','"');

Attempt 2
I have also tried search and replace in PHP with the below, with no luck as well

<?php
$str = ' “evil curly quotes“ no "good straight quotes"';
str_replace ('“', '"', $str);

echo $str;

// Prints:
// “evil curly quotes“ no "good straight quotes"
?>

Please help me anyone, there has to be an easy way to do this besides manually editing thousands of records?

هل كانت مفيدة؟

المحلول

You are not actually replacing any value. You forgot to assign the return value of the str_replace call to the $str variable. This will do the trick:

<?php
$str = ' “evil curly quotes“ no "good straight quotes"';
$str = str_replace ('“', '"', $str);

echo $str;

// Prints:
// “evil curly quotes“ no "good straight quotes"
?>

edit: Tom D also provided the correct answer in his comment (and did it earlier than me to be fair).

نصائح أخرى

// Replace smart or curly quotes, dashes and ellipses
// Replace UTF-8 characters.
$string = str_replace(
array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
array("'", "'", '"', '"', '-', '--', '...'),
$string);
// Replace Windows-1252 equivalents.
$string = str_replace(
array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
array("'", "'", '"', '"', '-', '--', '...'),
$string);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top