Number translation in php

Status
Not open for further replies.

erect

New member
Jun 3, 2007
3,795
154
0
Esoterica
twitter.com
I've got a list of numbers in Turkish (?) that I need to get translated into English. A simple str_replace won't work, I'm assuming php just doesn't recognize character set and ignores the matches.

This simple little script below attempts to do the translation for me but has no success. I left the iconv() in the code (but commented out) because that was my first thought, but didn't work.

PHP:
$number= '١٫٩٦' ;
// $number= iconv( "ISO-8859-9", "UTF-8", $number);

$number= str_replace('٠','0',$number) ;
$number= str_replace('١','1',$number) ;
$number= str_replace('٢','2',$number) ;
$number= str_replace('٤','4',$number) ;
$number= str_replace('٥','5',$number) ;
$number= str_replace('٦','6',$number) ;
$number= str_replace('٧','7',$number) ;
$number= str_replace('٨','8',$number) ;
$number= str_replace('٩','9',$number) ;

echo $number ;
Any help would be appreciated.

Ooh yea, can we get a little color change on the php coding here. That blue is tough to read ... yellow would do nicely
 


Are they multibyte characters like chinese? You might try the mb_* functions.

If speed isn't an issue, can you break up the string and treat it character by character, and when you come across those characters, recognize them somehow and insert the numeral instead?
 
If I were to guess, I'd say that's arabic... Not sure, but I think this is what they are: http://unicode.org/charts/PDF/U0600.pdf (it's a pdf).

On the 2nd page, the right-most column. It has the unicode codes. You could try replacing those symbols in your code with their unicode equivalents.

This may work unless I'm missing something.
 
Ah and what I just noticed is that in your str_replace function, you are declaring the numbers as strings by putting them in quotes. Maybe it's just that, although PHP usually doesn't take strings and integers too seriously. But try

Code:
$number= str_replace('٠', 0, $number);

without quotes around the numbers.

EDIT: Or, before using $number (after running str_replace), convert it to integer, just to make sure:
Code:
$number = (int)$number;
 
Tried on 3 servers and mb_ related functions don't exist .. if it comes down to it, I'll talk to my host. They must be pretty damn obscure for multiple hosts to not have them by default (PHP5+ for all 3).

I had high hopes for mb_eregi_replace

Are the Turkish numbers in the special character, "&#...;", format?

nope, their character equivalent. Running the characters through htmlspecialchars() produced ?? for each.

Bob, I guessed arabic too at first and got the translations to numbers via a arabic->english translator. Turkish is what I guess after digging around a bit more but really, the actual country is not important ... just the conversion process.

I put the 'quotes' around the character because I assumed it would not be a number to php .. just tried it w/o quotes and I guessed right

>> Parse error: syntax error, unexpected '?', expecting ')' <<

tried converting the ?? to an interger also, no dice

I'm looking at that PDF now

Thanks for the suggestions everyone
 
Status
Not open for further replies.