Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Detect if non-english [duplicate]

Tags:

php

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?

like image 596
Jessica Lingmn Avatar asked Apr 10 '13 08:04

Jessica Lingmn


Video Answer


1 Answers

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!";
    }
?>
like image 128
Edson Medina Avatar answered Oct 08 '22 23:10

Edson Medina