0

$str = str_replace ('–', '-', $str);不起作用(如我所愿,较长的 Unicode 破折号字符不会替换为减号字符。

$str 是从数据库中读取的,应该是 UTF-8 格式。

PHP 代码从 Apache 服务器运行。

我需要用减号替换所有这些长破折号。


$破折号=“–”;
回声“字符串:”。bin2hex($str) 。“,破折号:”。bin2hex($dash) 。"\n";
回声“字符串:”。$str 。“,破折号:”。$破折号。"\n";

string: 5a656c626f726166202623383231313b20d0bdd0bed0b2d18bd0b920d0bfd180d0b5d0bfd0b0d180d0b0d18220d0b4d0bbd18f20d0bbd0b5d187d0b5d0bdd0b8d18f20d0bcd0b5d0bbd0b0d0bdd0bed0bcd18b, dash: e28093
字符串:Zelboraf – новый препарат для лечения меланомы,破折号:–</pre>

出了什么问题(不是正确的 UTF-8):字符串还是破折号?

4

3 回答 3

3

这是一个编码为“-”的 HTML 实体 :-) 那是我的失败。

于 2012-09-27T14:12:11.423 回答
1
<?php

$str = 'Test–asd';

$old = '–';
$new = '!';

$str = str_replace ( $old, $new, $str );

echo $str;

?>

这对我来说很好:

输出:

Test!asd

似乎您有不同编码的问题,而不是 UTF8 字符更改。

于 2012-09-27T12:27:26.207 回答
0

EDIT:

try this:

$str = str_replace('\xe2\x80\x94', '-', $str);

Try:

$str = str_replace(chr(150), '-', $str);    // endash

or

$str = str_replace(chr(151), '-', $str);   // emdash

I think that the second one suits you more.

于 2012-09-27T12:10:06.427 回答