简体   繁体   English

®符号上的str_replace在PHP中不起作用

[英]str_replace on ® symbol does not work in PHP

Says it all in the title really. 真的在标题中说了所有。 The following code always returns false: 以下代码始终返回false:

$product = str_replace('®', '', $product);    

I have also tried searching for the html entities for the ® symbol. 我还尝试过搜索®符号的html实体。

Is there a solution for this? 有解决方案吗?

由于文件编码的原因,您必须将符号解码为其实体值( ®

$product = html_entity_decode(str_replace('®', '', htmlentities($product)));

I thing HD-'s answer is right but I'm thinking that this string with special characters are coming from a database and you don't have a quick way to transform the ® sign to its HTML representation &reg ; 我认为HD-的答案是正确的,但我认为带有特殊字符的字符串来自数据库,您没有快速的方法将®符号转换为HTML表示&reg ;

The problem you are getting may be originated by the fact that the database connection is using an encoding other than the encoding used by your php script. 您遇到的问题可能是由于数据库连接使用的编码不同于您的php脚本使用的编码所致。

If your PHP script is being saved in UTF-8, try the code below: 如果您的PHP脚本是以UTF-8保存的,请尝试以下代码:

$product = str_replace('®', '', utf8_encode($product));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM