简体   繁体   English

str_replace PHP不起作用

[英]str_replace PHP doesn't work

I'm trying to replace certain characters with some other strings I got it to work on my main page with this exact code but on another page it wont work. 我试图用某些其他字符串替换某些字符,但我却用这个确切的代码在我的主页上工作,但在另一页上它将无法工作。

In the database i Got this ±1¢2£¥3§4£ as description2 In my php i use 在数据库中,我得到了±1¢2£¥3§4£作为description2在我的PHP中,我使用

$description2 = mysql_result($product, 0, 'productDescription2');

if ($description2 !=""){
$searchArray = array('±', '¢', '£', '¥', '§');
$replaceArray = array('x', 'x', 'x', 'x', 'x');
$teknisk =  str_replace($searchArray, $replaceArray, $description2);}

And as the output i use echo ''. $teknisk .''; 作为输出,我使用echo ''. $teknisk .''; echo ''. $teknisk .'';

But the result is this 1 2 3 4 (The same as the original description2 ) 但是结果是 1 2 3 4 (与原始description2相同)

instead of x1x2xx3x4x 而不是x1x2xx3x4x

Please help! 请帮忙!

Perhaps database uses different encoding than your PHP file. 也许数据库使用的编码与PHP文件使用的编码不同。 Start with comparing those strings converted with bin2hex() function (in PHP). 首先比较用bin2hex()函数(在PHP中)转换的那些字符串。

Second attempt: 第二次尝试:

There are a bunch of suggestions in the PHP doc's comments. PHP文档的注释中有很多建议。 Try these: 试试这些:

  1. http://www.php.net/manual/es/function.str-replace.php#106653 http://www.php.net/manual/es/function.str-replace.php#106653
  2. http://www.php.net/manual/es/function.str-replace.php#75566 http://www.php.net/manual/es/function.str-replace.php#75566
  3. http://www.php.net/manual/es/function.str-replace.php#72344 http://www.php.net/manual/es/function.str-replace.php#72344

Try explicitly setting your page's chartype. 尝试显式设置页面的字符类型。 Try placing this in the header: 尝试将其放在标题中:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

Or, if you're using HTML 5: 或者,如果您使用的是HTML 5:

<meta charset="utf-8">

我认为,由于您有一些奇怪的字符,它们可能超出了ascii的范围,并且您可能需要使用多字节字符串函数,尽管它没有str_replace类似物,所以您可能需要使用多字节正则表达式。

please take a look at this , it will tell you how to handle UTF characters in a web app 请看一下,它将告诉您如何在网络应用中处理UTF字符

UTF-8 in Web Apps Web Apps中的UTF-8

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

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