1

In PHP, I have a string variable that contains a number. The string is a multibyte string and with UTF-8 encoding.

When I echo the variable($str), it prints 1392. But the problem is that calculations on this variable fails. Consider the output of the following lines:

echo $str; // Prints 1392

echo $str + 0; // Prints 0 (it should print 1392)

echo $str + 1; // Prints 1 (it should print 1393)

echo bin2hex($str); // Prints: dbb1dbb3dbb9dbb2
Fighter Jet
  • 407
  • 1
  • 5
  • 19
  • A UTF-8 encoded string containing "1392" should not be any different than as ASCII encoded string containing the same. Do you possibly have that number in *wide characters*? E.g. 1392. Show us `echo bin2hex($str)` – deceze Feb 21 '14 at 13:05
  • Works fine here: https://eval.in/104164 – Digital Chris Feb 21 '14 at 13:09
  • @deceze Thanks for your insight. I updated the code as you requested. – Fighter Jet Feb 21 '14 at 14:02
  • What you have there are the Arabic-Indic numerals "۱۳۹۲". PHP can't do math with those! – deceze Feb 21 '14 at 14:17
  • @deceze The number in string format is the return of a function, a helper function not mine. I have no control over the return of the function. Is this a real blockage? Or any way you could help break this? I appreciate it. – Fighter Jet Feb 21 '14 at 14:35
  • You'd have to do a `str_replace` to replace each numeral with the corresponding 1, 2, 3 etc, *then* you can do math with it. Does this really output plain "1392" for you instead of "۱۳۹۲"?! – deceze Feb 21 '14 at 14:41
  • @deceze The output is NOT plain "1392". The second output you mentioned is right. – Fighter Jet Feb 21 '14 at 14:46
  • @deceze Is this question of any help? [Convert Numbers](http://stackoverflow.com/questions/3386835/convert-english-numbers-to-arabic-numerals) – Fighter Jet Feb 21 '14 at 14:47
  • Yes, pretty much this answer is what you want, just in reverse: http://stackoverflow.com/a/3387060/476 I don't know Arabic numerals in detail either to supply you with a solution in code. – deceze Feb 21 '14 at 15:09
  • @deceze I fixed this issue with your great help. You acted like a constant friend. – Fighter Jet Feb 21 '14 at 15:24
  • @deceze sorry brada for what I told.. I just assumed it as a normal string and I posted the answer.. And I would also like to know how to resolve this.. :) – Nishant Solanki Feb 21 '14 at 17:37

0 Answers0