Auto Text Direction Detector v1.0


Provides smart direction detection of any text (mixed or not) - LTR or RTL


All world languages are supported!


Up Buy NOW
on CodeCanyon


Why we need to detect the text direction?
Please look at the two examples below



The below is wrong :( Without detecting the RTL direction on a LTR container, notice how the dot which is placed at the end of the sentence - showed up at the beginning of the sentence.

هذا النص باللغة العربية.

טקסט זה ב עברי.

این متن به زبان فارسی.



The below is correct :) With a RTL text direction detection on a LTR container, notice how the dot which is placed at the end of the sentence is showed up in its correct place.

هذا النص باللغة العربية.


טקסט זה ב עברי.


این متن به زبان فارسی.







Using this script is very easy, you just need to call a method that will bring you the result as below:
// Calling the method in the class
$direction_detector->getTextDirection($string); // RTL or LTR

// The result, we get two CSS attributes as below:

// The CSS text direction property (direction) which is either RTL or LTR
$textDirection = $direction_detector->textDirection;

// The CSS text align property (text-align) which is either right or left
$textAlign = $direction_detector->textAlign;




The script also detects the direction of a mixed-language text based on three scenarios:

Case 1: If the first word in a string is RTL followed by any number of LTR words like English, Spanish, Italian, etc. then the final text direction is RTL.

Case 2: If the string has got 4 LTR words at the beginning followed by a 1 RTL word, then the final direction is RTL.

Case 3: If the string has 5 LTR words or more followed by 1 RTL word, the total direction is LTR.



Below are the three cases explained:
Case 1: The script detected a RTL direction, because the first word is RTL, so whatever LTR text followed, it will not affect

مرحبا This is the first case



Case 2: The script detected a RTL direction, because there are only 3 LTR words followed by the Arabic text (RTL text)

This is mixed text هذا نص مختلط



Case 3: The script detected a LTR direction, because there are more than 4 LTR words followed by the Arabic text (RTL text)

This is mixed text contains English and Arabic هذا نص مختلط يحتوي العربية والإنجليزية







Contact Us
We'd be very happy to hear from you!







Muwakaba @ 2024