- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Detect language from string in PHP
The language can’t be detected from the character type. There are other ways, but they don’t guarantee complete accuracy. The ‘TextLanguageDetect Pear Package’ can be used with decent accuracy. Below is a sample code for the same −
Example
require_once 'Text/LanguageDetect.php'; $l = new Text_LanguageDetect(); $result = $l->detect($text, 4); if (PEAR::isError($result)) { echo $result->getMessage(); } else { print_r($result); }
Output
This will produce the following output −
Array ( [german] => 0.407037037037 [dutch] => 0.288065843621 [english] => 0.283333333333 [danish] => 0.234526748971 )
It is easy to use and has 52 language databases. But the downside is that the Eastern Asian languages can’t be detected using this package.
- Related Articles
- Detect base64 encoding in PHP?
- How to Detect User Timezone in PHP?
- Remove comma from a string in PHP?
- Remove new lines from string in PHP
- Simplest way to detect client locale in PHP
- How to detect search engine bots with PHP?
- PHP – How to detect character encoding using mb_detect_encoding()
- PHP – Detect HTTP input character encoding with mb_http_input()
- Detect Capital in a given string using C++
- How to get parameters from a URL string in PHP?
- Convert timestamp coming from SQL database to String PHP?
- PHP program to remove non-alphanumeric characters from string
- Get unix timestamp from string without setting default timezone in PHP
- How to delete the vowels from a given string using C language?
- What are string literals in C language?

Advertisements