在php中将普通引号更改为卷曲引号

在php中将普通引号更改为卷曲引号,php,html,regex,Php,Html,Regex,我正在尝试将直接引号(“某物”)改为卷曲引号(“某物”)在PHP中。其他答案不适用于我的情况,因为我有一个作为变量从DB导入的产品详细信息,使用stru-replace我只设法将其更改为。,似乎我无法将第二个更改为“。据我所知,没有办法做到这一点 例如: $description outputs->“大家好”,我想把这个直接的“引号”改为“卷曲的” 我想要的是: $description outputs->Hello“everyone”,我想“将”这个直接的“引号”改为“curly”引号。尝试使

我正在尝试将直接引号(“某物”)改为卷曲引号(“某物”)在PHP中。其他答案不适用于我的情况,因为我有一个作为变量从DB导入的产品详细信息,使用
stru-replace
我只设法将其更改为,似乎我无法将第二个更改为。据我所知,没有办法做到这一点

例如:

$description outputs->“大家好”,我想把这个直接的“引号”改为“卷曲的”

我想要的是:


$description outputs->Hello“everyone”,我想“将”这个直接的“引号”改为“curly”引号。

尝试使用
preg\u replace
替换模式
“(.*?”
。然后,在curly quotes中替换为捕获组
$1

$input = "Hello \"everyone\", I would like to \"change\" this straight \"quotes\" to \"curly\" ones.";
$output = preg_replace("/\"(.*?)\"/", "„$1“", $input);
echo $output;
这张照片是:

Hello „everyone“, I would like to „change“ this straight „quotes“ to „curly“ ones.
Exklusiv von buttinette: Baumwollstoff “Leo”,
编辑:

您正在尝试替换已编码双引号的HTML代码,因此请尝试以下操作:

$input = "Exklusiv von buttinette: Baumwollstoff "Leo",";
$output = preg_replace("/"(.*?)"/", "“$1”", $input);
echo $output;
这张照片是:

Hello „everyone“, I would like to „change“ this straight „quotes“ to „curly“ ones.
Exklusiv von buttinette: Baumwollstoff “Leo”,

使用
explode
array\u reduce

$str = 'Hello "everyone", I would like to "change" this straight "quotes" to "curly" ones.';

$parts = explode('"', $str); // or  explode('"', $str);
$carry = array_shift($parts);

$result = array_reduce($parts, function ($c,$i) {
    static $up = false;
    return $c . ((true === $up=!$up) ? '„' : '“') . $i;
}, $carry) ;

显然,如果原始引号是html实体,则必须更改
explode
的第一个参数


使用strtok:

$str = 'Hello "everyone", I would like to "change" this straight "quotes" to "curly" ones.';

$result = substr(strtok(".$str", '"'), 1);

while (false !== $part = strtok('"')) {
    $result .= "„${part}“" . strtok('"');
}

我可能错了,但“卷曲”引号不只是一种字体样式吗?不是,可以通过在css中指定q{quotes:“\”;}来实现。但我们有一个定制的CMS,其中至少有15-20k个产品。想象一下,将每个产品从直引号更改为它不会改变任何东西。可能是因为直引号只是",不带\.@asobak使用双引号表示的PHP字符串中的文本双引号需要用反斜杠转义。我的代码针对的是您在问题中提供的示例数据。这是正确的,我知道。但不应该有不起作用的理由。我已经尝试过类似的方法,但我针对引号而不是word..然后给我答案失败的示例数据。您是在HTML文本上执行此操作的,而不是在呈现的输出上。请尝试我的更新答案。