0

I have a php feedback form that I want the comment text to be aligned to left or right depending on language: if Arabic should be aligned rtl, any other language no (default which is ltr)

This is the current code line (that makes everything right to left!):

$comment = "<p dir=\"rtl\">".str_replace ("\n", "<br />", $comment);

so I want to tell it: if Arabic, then rtl, if not Arabic, ignore that rtl

Mike
  • 2,051
  • 4
  • 28
  • 46

2 Answers2

1
function containsArabic($str)
{
    return preg_match('~\p{Arabic}~u', $str);
}

echo containsArabic('helloسلام'); //  rturn 1
echo containsArabic('سلام');//return 1
echo containsArabic('testسلامtest'); // return 1
echo containsArabic('test');// return 0

if the text contain Arabic letter the function return 1 else return 0

Adnen Chouibi
  • 400
  • 4
  • 16
0

You can use these regex'es to determine whether the comments contain Arabic or Persian chars.

function containsArabic($str)
{
    return preg_match('~\p{Arabic}~u', $str);
}

echo containsArabic('helloسلام');
echo containsArabic('سلام');
echo containsArabic('testسلامtest');
echo containsArabic('test');
Community
  • 1
  • 1
VahidN
  • 18,457
  • 8
  • 73
  • 117
  • thanks for the reply.. however I am new to this.. can you tell me what should I add to the above code to make it work? – Mike Sep 07 '12 at 20:40
  • I've edited the answer: http://ideone.com/5zFPC, just test your input and set the dir based on that. – VahidN Sep 08 '12 at 10:58