1

Why do I get 2 different results in this very simple script:

$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© ) 

I don't understand why the second explode won't split up the string just because i originated from a mysql query.

Edit: I can't even use str_replace on this example:

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

Makes no sense. utf8_decode changes nothing. Not even if I save it in a variable first e.g.:

$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©

Edit 3

No solution. Had nothing to do with the limit. If I exchange the © with a B then everything works.

Solution:

I had to included correct encoding (I simply added the standard html headers and encoding).

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Emily Adler
  • 73
  • 2
  • 12

1 Answers1

2

It does not work because the encodings differ. The "©" in your source code is represented using different bytes than the string you get from MySQL, so they do not match and explode doesn't find any substring to explode on. You need to ensure you're using the same encoding throughout, or convert encodings as needed.

See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and UTF-8 all the way through.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889