Hi,
As I mentioned in my original posting, I am not a PHP programmer; But,
it seems that for keyboard input functions you WILL have to convert
UTF-8 to HTML encoding if you want to use your function; for example
by calling a function like htmlentities() or utf8_decode() before
calling utf8_to_int(). The pseudo code looks like this:
if (input is in UTF-8) { # for example input is from a Linux keyboard
$str = htmlentities($str, ENT_COMPAT, "UTF-8" );
}
$intvalue = utf8_ot_int($str);
I've included the function again; I added the extra code to check for "x in set y"
test. Also note the use of the built-in strtr() function. These builtins usually
are more efficient (written in C) and are faster. This code has not been tested.
function utf8_to_int($str)
{
$transtbl = array (
"۰" => '0',
"۱" => '1',
"۲" => '2',
"۳" => '3',
"۴" => '4',
"۵" => '5',
"۶" => '6',
"۷" => '7',
"۸" => '8',
"۹" => '9'
);
foreach ($transtbl as $key => $value) {
if ($key == $str) { # it has an HTML
encoded numeric
$str = strtr($str, $transtbl); # convert all of them to ASCII
[0-9]
return (int) $str; # convert to whole
thing to integer
}
}
return (int) $str;
}
One last thing, this function (and your version) has a fatal flaw. Do you
want to guess what it is?
Hint: What happens if the string is "ABCDE"? What is the difference in the
return value from the function for the strings "ABCD" and "۰"?
-Fariborz
--- Begin Message ---
Hi dears.
Mr.Tavakkolian was helping me until my function completed.
This function converts utf8(digit) to integer.
$farsi_table_linux=array("NONE",
"۰",
"۱",
"۲",
"۳",
"۴",
"۵",
"۶",
"۷",
"۸",
"۹");
function search_index_array($str)
{
global $farsi_table;
for ($i=0;$i<11;$i++)
{
if ($farsi_table[$i]==$str )
return $i;
}
return FALSE;
}// end of search_index_array
////////////////////////////////
function utf8_to_int($str)
{
$len=strlen($str);
$out="";
$char=explode(";",$str);
for ($i=0;$i<$len;$i++)
{
$char[$i].=";";
if (search_index_array($char[$i])!=False)
$out.=search_index_array($char[$i])-1;
}//end of for ($i)
return $out;
}//end of utf8_to_int
When i call utf8_to_int("۰"."۹") ,this return 09 ,But When i enter a
number(utf8) via keyboard,This function return 0.
Please guide me that i how enter a number via keyboard(persian) & i get true answer.
--regards
_____________________________________________________________
Thank you for choosing LinuxQuestions.
http://www.linuxquestions.org
_______________________________________________
FarsiWeb mailing list
[EMAIL PROTECTED]
http://lists.sharif.edu/mailman/listinfo/farsiweb
--- End Message ---
_______________________________________________
FarsiWeb mailing list
[EMAIL PROTECTED]
http://lists.sharif.edu/mailman/listinfo/farsiweb