javascript-php text array problem of making Chinese verification Code

Source: Internet
Author: User
Keywords Php javascript python
在做一个php中文验证码时,按原本的思路是将一段文字分割成一个个文字组成个文字数组,然后遍历四次,随机生成四个验证文字,然后画在image上,可是发现用`str_split($str,3)`来分割有时会少一个文字,换成`explode(" ",$str)`还是偶尔缺字。。。主要代码如下:` $fontFace='aa.ttf';    $str=" 大 闸 是 中 国 主 导 品 牌 亦 行 业 协 会 重 点 推 荐 家 集 养 殖 与 销 售";    $strDb=str_split($str,3);for($i=0;$i<4;$i++){    $fontColor=imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));    $index=mt_rand(0,count($strDb)+1);    $cn=$strDb[$index];    $captcha.=$cn;    $x=($i*120/4)+mt_rand(5,10);    $y=mt_rand(25,45);    imagettftext($img,15,mt_rand(-30,60),$x,$y,$fontColor,$fontFace,$cn);}`感觉是文字编码的问题,字节数设为4的时候也可以显示部分文字,可是具体怎么解决不知道,求帮助,谢谢!

Reply content:

在做一个php中文验证码时,按原本的思路是将一段文字分割成一个个文字组成个文字数组,然后遍历四次,随机生成四个验证文字,然后画在image上,可是发现用`str_split($str,3)`来分割有时会少一个文字,换成`explode(" ",$str)`还是偶尔缺字。。。主要代码如下:` $fontFace='aa.ttf';    $str=" 大 闸 是 中 国 主 导 品 牌 亦 行 业 协 会 重 点 推 荐 家 集 养 殖 与 销 售";    $strDb=str_split($str,3);for($i=0;$i<4;$i++){    $fontColor=imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));    $index=mt_rand(0,count($strDb)+1);    $cn=$strDb[$index];    $captcha.=$cn;    $x=($i*120/4)+mt_rand(5,10);    $y=mt_rand(25,45);    imagettftext($img,15,mt_rand(-30,60),$x,$y,$fontColor,$fontFace,$cn);}`感觉是文字编码的问题,字节数设为4的时候也可以显示部分文字,可是具体怎么解决不知道,求帮助,谢谢!


Two points, the first is $str without spaces separate, the second is to generate random index when, should be-1, not +1
When you take a random value in an array like this, index can use a array_rand() function to generate
The following changes are completed

$fontFace='aa.ttf';    $str="大闸是中国主导品牌亦行业协会重点推荐家集养殖与销售";    $strDb=str_split($str,3);for($i=0;$i<4;$i++){    $fontColor=imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));    $index=array_rand($strDb);    $cn=$strDb[$index];    $captcha.=$cn;    $x=($i*120/4)+mt_rand(5,10);    $y=mt_rand(25,45);    imagettftext($img,15,mt_rand(-30,60),$x,$y,$fontColor,$fontFace,$cn);}

Use the MB extended function to handle the string, solve the coding problem, what is the use of the other space?

If the $STR contains Chinese and English with this division$strDb=preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.