0

there is a function for converting English standard nums to Persian (Farsi) or Arabic nums:

function farsinum($str){
  if (strlen($str) == 1){
    $str = "0".$str;
    $out = "";
    for ($i = 0; $i < strlen($str); ++$i) {
      $c = substr($str, $i, 1); 
      $out .= pack("C*", 0xDB, 0xB0 + $c);
    }
  }
  return $out;
}

But this function produce 01 02 03 ... instead of 1 2 3 ... I think something must be change here:

$out .= pack("C*", 0xDB, 0xB0 + $c);

Any help appreciated.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
Vahid
  • 382
  • 1
  • 6
  • 19

1 Answers1

1

This function will convert a single digit:

function farsinum ($digit) {
  return pack("C*", 0xDB, 0xB0 + substr($digit, 0, 1));
}

If you want to convert a number larger than 9, a simple loop will fix that:

function farsinum ($number) {

  // Work with a string so we can use index access
  $number = (string) $number;

  // We'd better add some error checking, to make sure we only accept digits
  if (!ctype_digit($number)) return FALSE;

  // Loop characters and convert them
  for ($i = 0, $out = '', $length = strlen($number); $i < $length; $i++) {
    $out .= pack("C*", 0xDB, 0xB0 + $number[$i]);
  }

  // Return the result
  return $out;

}

I can't find a resource to explain how negative numbers and floats are expressed in Farsi text, if you need to do it then let me know how you want the output to look and I will have a go.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • thank's a lot,yes i was undrestand it, i used if else to solve it! numbers in Farsi is more like to English 1 2 3... we have ۱ ۲ ۳ and negative -1 -2 -3 looks like -۱ -۲ -۳, we always make calculation in English then show it in Persian,however your code helped me out very much. sorry for my poor English, i can't speak well but understand very well ;-) – Vahid May 23 '12 at 12:40