Skip to content

Instantly share code, notes, and snippets.

@khal3d
Last active September 15, 2022 03:26
Show Gist options
  • Save khal3d/4648574 to your computer and use it in GitHub Desktop.
Save khal3d/4648574 to your computer and use it in GitHub Desktop.
Check if there RTL characters (Arabic, Persian, Hebrew)
<?php
/**
* Is RTL
* Check if there RTL characters (Arabic, Persian, Hebrew)
*
* @author Khaled Attia <[email protected]>
* @param String $string
* @return bool
*/
function is_rtl( $string ) {
$rtl_chars_pattern = '/[\x{0590}-\x{05ff}\x{0600}-\x{06ff}]/u';
return preg_match($rtl_chars_pattern, $string);
}
// Arabic Or Persian
var_dump(is_rtl('نص عربي أو فارسي'));
// Hebrew
var_dump(is_rtl('חופש למען פלסטין'));
// Latin
var_dump(is_rtl('Hello, World!'));
@vishal-bansal
Copy link

it's relay good.. and working !!

@iLenTheme
Copy link

As valid if the entire website is RTL?

@giladfit
Copy link

need fix in line 20
// Hebrew
var_dump(is_rtl('גם ישו דיבר עברית'));

@BashrO
Copy link

BashrO commented Apr 22, 2015

You wrote Anti-Semitic line in the Hebrew, I can guess it's in purpose since you're from Egypt..... lame move.

@hamidhaghdoost
Copy link

hamidhaghdoost commented Nov 8, 2016

Thanks for your great work, How I can know a text is completely Farsi or not, and how I can remove non Persian characters from a text?

@mghayour
Copy link

hi, thanks for this work
we have problem if string contains both LTR and RTL characters
i made it fuzzy, that support mixed RTL&LTR strings
https://gist.github.com/mghayour/29899988098a5395a33af8763f92eded

@reza19
Copy link

reza19 commented Mar 31, 2019

thanks . its usefull

@noorcs
Copy link

noorcs commented May 30, 2019

Thanks for sharing is_rtl function with us.
Its very fast. I just tested it and the following are results.

// Total Execution Time for is_rtl: 0.34484505653381 Seconds for 100000 iterations
as compared to my own function
// Total Execution Time for my_is_rtl: 12.653607130051 Seconds for 100000 iterations

@Shoukat-bjr
Copy link

Thanks for your great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment