S.M_Emamian S.M_Emamian - 7 months ago 20
PHP Question

how to check if a string has english letters or not

there are many questions on stackoverflow that them say if a string contains only english letters. but I would like to know how can I to check if a string has english letters :

ex:

$string="で書くタッチイベント B A(フ";

if(??? $string)
//this string has some english letters .

Answer

Use preg_match()

Example :-

<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

wrt question it will be like :-

<?php
$string = "で書くタッチイベント B A(フ";
$pattern = '/[a-zA-Z]/';
preg_match($pattern, $string, $matches);
print_r($matches);
?>