Use php to count the number of Chinese and English characters in a string

  • 2020-06-15 07:57:46
  • OfStack


<?php
echo $str = "43fdf test fdsfadaf43543543 The worker asks guard against theft guard against theft lock 5345gfdgd";
preg_match_all("/[0-9]{1}/",$str,$arrNum);
preg_match_all("/[a-zA-Z]{1}/",$str,$arrAl);
preg_match_all("/([/x{4e00}-/x{9fa5}]){1}/u",$str,$arrCh);
echo "<pre>";
echo " Digital number :".count($arrNum[0])."<br/>";
echo " Number of letters :".count($arrAl[0])."<br/>";
echo " The number of Chinese :".count($arrCh[0]);
?>

Related articles: