Jump to content

Recommended Posts

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

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.

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.

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));
}

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);
}

 

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

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

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

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.