Is there a way to detect the language of the data being entered via the input field?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
hmm i may offer an improved version of DimaKrasun's function:
okay, enough joking!
Pekkas suggestion to use the google translate api is a good one! but you are relying on an external service which is always more complicated etc.
i think Rushyos approch is good! its just not that easy. i wrote the following function for you but its not tested, but it should work...
final thoughts: as you see i added for example a latin counter, the range is just a dummy number b ut this way you could detect charsets (hebrew, latin, arabic, hindi, chinese, etc...)
you may also want to eliminate some chars first... maybe @, space, line breaks, slashes etc... the PREG_SPLIT_NO_EMPTY flag for the preg_split function would be useful but because of the bug I didn't use it here.
you can as well have a counter for all the character sets and see which one of course the most...
and finally you should consider chopping your string off after 200 chars or something. this should be enough to tell what character set is used.
and you have to do some error handling! like division by zero, empty string etc etc! don't forget that please... any questions? comment!
if you want to detect the LANGUAGE of a string, you should split into words and check for the words in some pre-defined tables. you don't need a complete dictionary, just the most common words and it should work fine. tokenization/normalization is a must as well! there are libraries for that anyway and this is not what you asked for :) just wanted to mention it
This function checks whether the entered line/sentence is arabic or not. I trimmed it first then check word by word calculating the total count for both.
Use regular expression for shorter and easy answer
This will return true (1) for arabic string and 0 for non arabic string
You can use function, which i have written for you:
But please, check it, before use.
[EDIT 1]
Your question: "How do I detect if an input string is Arabic?" And i have answered to it, what`s wrong?
[EDIT 2]
Read this - Detect language from string in PHP
[EDIT 3]
Excuse, i rewrite function to this, try it:
I assume you're referring to a Unicode string... in which case, just look for the presence of any character with a code between U+0600–U+06FF (1536–1791) in the string.