1

I'm trying to build a webpage where two types of users come to my web site. One is the usual English users, and the second one is Persian users ( same text attributes as Arabic, like text direction and text alignment ). So I want to be able to set the text direction of echoed texts depending on users input.

Let me give you an example ;

I have a text input like this :

<form name="myform" action="thispage.php" method="post" target="_self">
<input type="text" id="decsription" name="description" value="" />
<input type="submit" name="submit" value="SUBMIT" >
</form>

<?php 

if (isset($_POST['description']))
{
echo '<div align="right" dir="rtl"> Your Chiose is : '.$_POST['description'].'</div>';
}

SO, for example if the text input is typed in english, the echo section would change to something like this :

echo '<div align="left" dir="ltr"> Your Chiose is : '.$_POST['description'].'</div>';

But if the text is non English, then the echo section would be like this :

echo '<div align="right" dir="rtl"> انتخاب شما : '.$_POST['description'].'</div>';

Is this possible to do ?

Thanks for your help.

Siwa
  • 103
  • 2
  • 8
  • 1
    The question at http://stackoverflow.com/questions/1441562/detect-language-from-string-in-php might be of use here. – n8schloss Jun 11 '12 at 18:14
  • so what ur essentially asking is how to detect typed language with php? – arijeet Jun 11 '12 at 18:14
  • @redskins80 yes, and then show the text depending on that detected language. – Siwa Jun 11 '12 at 18:16
  • 1
    you could try and use some web API which would be more thorough with their checking.. http://detectlanguage.com/ http://www.alchemyapi.com/api/lang/ – arijeet Jun 11 '12 at 18:18

1 Answers1

3
function check_eng($str)
{
if (strlen($str) != strlen(utf8_decode($str))) {
return false;
} else {
return true;
}
}

check_eng('hello'); would return true, while check_eng('で書くタッチイベント (フ'); would return false.

Works 99% of the time.

Demonstration: http://codepad.viper-7.com/k0PlKU

For your situation, you'd just check the string with this function, and if it returned false use the right-to-left orientation.

Steve
  • 632
  • 4
  • 16