Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top