k1k Posted May 3, 2011 Share Posted May 3, 2011 Anyone know how to cast arabic numeric input to (int) ? Or how to convert arabic numerals to english numerals? I created an auction website, inputting english numbers seems to work. However when I switch languages to Arabic, it doesn't work. By casting any arabic numeral input to (int) It keeps returning 0. ١٢٣٤٥٦٧٨٩ 123456789 Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/ Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 Not everybody can read Arabic. I certainly can't. And guess what: neither can PHP. Try using strtr an an array (not two strings) to convert every Hindi/Arabic numeral to an Arabic/English numeral. Then you can probably convert it. Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210080 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 Try something like this: function indicToArabic($str) { $arabic = array('0','1','2','3','4','5','6','7','8','9'); $indic = array('०','१','२','३','४','५','६','७','८','९'); return (int)str_replace($indic, $arabic, strrev($str)); } I'm assuming you need to reverse the string, but if that's not an issue remove the strrev() function call. Let me know if this works out. Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210083 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 Note that the forum went and converted all the characters there to html entities. Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210088 Share on other sites More sharing options...
k1k Posted May 3, 2011 Author Share Posted May 3, 2011 First of all, thank you all for these quick and helpful responses. Attempted this first, no luck getting it to work. $numInput = $_POST['check']; $trans = array("٠" => "0", "١" => "1", "٢" => "2", "٣" => "3", "٤" => "4", "٥" => "5", "٦" => "6", "٧" => "7", "٨" => "8", "٩" => "9"); echo strtr($numInput, $trans); Then attempted to use this function (Reversing the numbers isnt really needed) - still not working for some reason. I tried to remove (int) to get a string version ( no luck ) - Then i replaced the #2410 etc. with the actual arabic numbers and still cant get it to work :s function indicToArabic($str) { $arabic = array('0','1','2','3','4','5','6','7','8','9'); $indic = array('०','१','२','३','४','५','६','७','८','९'); return (int)str_replace($indic, $arabic, ($str)); } Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210089 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 This might be an alternative: function indicToArabic($str) { $arabic = array('0','1','2','3','4','5','6','7','8','9'); $indic = array('\xD9\xA0','\xD9\xA1',...... etc); // Should be D9A0 to D9A9 return (int)str_replace($indic, $arabic, $str); } Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210092 Share on other sites More sharing options...
k1k Posted May 3, 2011 Author Share Posted May 3, 2011 Thanks for responding, attempted to make it cast only one numeral, not working for some reason. #1 function indicToArabic($str) { $arabic = array('1'); $indic = array('\xD9\xA1'); return (int)str_replace($indic, $arabic, $str); } #2 function indicToArabic($str) { $arabic = array('1'); $indic = array('١'); return (int)str_replace($indic, $arabic, $str); } Also thought of breaking the number string into single characters then switching each arabic number with an english number like so: function indicToArabic($str) { switch($str){ case '١': return 1; break; } } For some reason that also failed Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210104 Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 What does echo bin2hex($_POST['check']); (or whatever variable) produce? Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210118 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 Let's try this once more: function indicToArabic($str) { $arabic = array('0','1','2','3','4','5','6','7','8','9'); $indic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'); return (int)str_replace($indic, $arabic, $str); } $str = '١١٢٥'; echo $str . PHP_EOL; echo "Number: " . indicToArabic($str) . PHP_EOL; I get: ١١٢٥ Number: 1125 Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210124 Share on other sites More sharing options...
k1k Posted May 3, 2011 Author Share Posted May 3, 2011 Thank you very much! Works perfectly! Truly Appreciate it! Let's try this once more: <?php function indicToArabic($str) { $arabic = array('0','1','2','3','4','5','6','7','8','9'); $indic = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'); return (int)str_replace($indic, $arabic, $str); } $str = '١١٢٥'; echo $str . PHP_EOL; echo "Number: " . indicToArabic($str) . PHP_EOL; I get: ١١٢٥ Number: 1125 Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210125 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 Well it seems I'm unable to get the forum to accept these characters. I'll put the script in webspace. It does work fine if you set the format of the source file to be utf-8 and you have the actual utf-8 characters in the source array. http://www.gizmola.com/blog/uploads/testarabic.txt Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210127 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 So what we learned is that what you are getting as input is already being translated to the html entities rather than the utf-8 characters. I'm not sure if that's a good thing or not, but you probably want to explore that further. Quote Link to comment https://forums.phpfreaks.com/topic/235448-php-cant-cast-arabic-numeric-input-as-int/#findComment-1210129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.