9

I am using these code to check if a string is in English or not.

<?php
    $string="で書くタッチイベント (フ";

    if(!preg_match('/[^\W_ ] /',$string)) {
        echo "Please enter English words only:(";
    } else {
        echo "OK, English Detected!";
    }
?>

It cant provide perfect result because string like "some english text で書くタッチイベント (フ" this also detects as English language, any idea?

Edson Medina
  • 9,862
  • 3
  • 40
  • 51
Jessica Lingmn
  • 1,162
  • 3
  • 10
  • 15
  • Define "english". For letters only the trivial `/^[a-zA-Z]*$/` would only match "english". – Jon Apr 10 '13 at 08:59
  • Do you mean 'actual English' or do you mean 'containing more Latin characters than kanji/kana'? – Rup Apr 10 '13 at 09:02

3 Answers3

36

Try this (please note you need mbstring php module installed):

<?php
    $string="で書くタッチイベント (フ";

    if(strlen($string) != mb_strlen($string, 'utf-8'))
    { 
        echo "Please enter English words only:(";
    }
    else {
        echo "OK, English Detected!";
    }
?>
Edson Medina
  • 9,862
  • 3
  • 40
  • 51
0

You can not detect the language from the character type. And there are no foolproof ways to do this.

With any method, you're just doing an educated guess. From: Detect language from string in PHP

Though some of the below articles may get handy in case..

http://papermashup.com/php-language-detection/

https://github.com/BruceJillis/PHP-Language-Detection

http://phpir.com/language-detection-with-n-grams/

Hope it helps..

Community
  • 1
  • 1
Hiren Pandya
  • 989
  • 1
  • 7
  • 20
  • 1
    Does this answer say anything not copy/pasted from that other question? – Jon Apr 10 '13 at 09:01
  • Why do you guys downvote his comments? It's absolutely true... – Chris Apr 10 '13 at 09:01
  • @Chris I did not downvote, but comments should not be posted as answers. Also, if Hiren believes this question is already answered elsewhere, than he/she should mark the OP as duplicate. – Boaz Apr 10 '13 at 09:03
  • @Chris: Because there is already a mechanism for directing users to answers that someone else has already provided: voting to close the question as a duplicate. Even if you don't want to do that, instead of *copy/pasting* from the answers of others one can always leave a comment linking to them. – Jon Apr 10 '13 at 09:03
  • Courtesy has been given... And I had some articles so shared as the code is much larger.. – Hiren Pandya Apr 10 '13 at 09:03
  • @HirenPandya If you believe you have further to contribute to this topic, than you should post a new answer to the original question and mark the current question as duplicate. – Boaz Apr 10 '13 at 09:05
  • I can not mark it as duplicate.. Rather than down voting it and discourage the OP, I had some of the articles which is not posted in the given stackoverflow link.. So posted. But next time, I will keep this in mind... Thank you. – Hiren Pandya Apr 10 '13 at 09:06
  • @HirenPandya Why can't you flag the OP? – Boaz Apr 10 '13 at 09:07
  • I do not have that privilege to perform mentioned action... – Hiren Pandya Apr 10 '13 at 09:09
  • @HirenPandya Flagging for moderator attention is a privilege given to any user with 15 reputation points and up. – Boaz Apr 10 '13 at 09:09
  • But it is not available to me though.. Tried so many times.. People here, told me that you should not post only link to the duplicate question so I put some my efforts to provide a better answer... – Hiren Pandya Apr 10 '13 at 09:11
0
if(!preg_match('/[^\w ]/u',$string))
Amir
  • 4,089
  • 4
  • 16
  • 28