【问题标题】:Convert Microsoft Curly Quotes to Straight Quotes in PHP在 PHP 中将 Microsoft 弯引号转换为直引号
【发布时间】:2013-05-28 22:25:43
【问题描述】:

我很难尝试修复客户数据库记录。我需要用直引号" 查找和替换所有看起来像这样 的弯引号

尝试 1
我试图在我的 MySQL 数据库上运行它,但没有成功。

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

尝试 2
我也试过用下面的PHP搜索和替换,也没有运气

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

echo $str;

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

请任何人帮助我,除了手动编辑数千条记录之外,还必须有一种简单的方法吗?

【问题讨论】:

  • 它们是存储为大引号还是 html 代码等效? &amp;#147;
  • @PhilCross 不幸的是,它们存储为实际的大引号,用于 HTML 链接,导致链接无法正常工作
  • 不幸的是,我不能再帮忙了!也许这个网站会给你一个线索? toao.net/48-replacing-smart-quotes-and-em-dashes-in-mysql
  • 我不认为 PHP 会根据您的操作改变 $str 的值。试试 $str=str_replace(...)
  • @TomD 你是对的,好眼力,谢谢你的工作

标签: php mysql double-quotes smart-quotes


【解决方案1】:

您实际上并没有替换任何值。您忘记将str_replace 调用的返回值分配给$str 变量。这样就可以了:

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

echo $str;

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

编辑:Tom D 在他的评论中也提供了正确的答案(为了公平起见,他比我更早做了)。

【讨论】:

  • @TomD 我在回答中添加了免责声明。
【解决方案2】:
// 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);

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多