如何爆炸工作?

时间:2015-11-20 09:44:45

标签: php string split explode

为什么我在这个非常简单的脚本中得到2个不同的结果:

$str = "114004©301000©301000©301000©";
echo $str."<br/ >"; //Prints 114004©301000©301000©301000©
$test1 = (explode("©",$str));
print_r($test1); //Prints Array ( [0] => 114004 [1] => 301000 [2] => 301000 [3] => 301000 [4] => ) 

echo "<br /><br />";

$blah = $data['final_alle_produktid']; // Contains a string called 114004©301000©301000©301000©
$test1 = (explode("©",$blah));
print_r($test1); //Prints Array ( [0] => 114004©301000©301000©301000© ) 

我不明白为什么第二次爆炸不会因为我来自mysql查询而拆分字符串。

修改:在此示例中我甚至无法使用str_replace

$blah = str_replace("©"," ",$data['final_alle_produktid']); 
echo $blah."<br/ >"; //Still prints 114004©301000©301000©301000©

没有任何意义。 utf8_decode什么都没改变。即使我先将它保存在变量中,例如:。

$test = $data['final_alle_produktid'];
$blah = str_replace("©"," ",$test); 
echo $blah."<br/ >"; //Still prints 114004©301000©301000©301000©

EDIT2:

print_r($blah); //Prints 114004©301000©301000©301000©

编辑3

没有解决方案。与限制没有任何关系。如果我用B交换©,那么一切正常。

解决方案:

我必须包含正确的编码(我只是添加了标准的html标头和编码)。

1 个答案:

答案 0 :(得分:2)

它不起作用,因为编码不同。源代码中的“©”使用的字节数与您从MySQL获得的字符串不同,因此它们不匹配,explode找不到任何要爆炸的子字符串。您需要确保在整个过程中使用相同的编码,或根据需要转换编码。

请参阅What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With TextUTF-8 all the way through

相关问题