Question about the text array for making a Chinese verification code using javascript-php

Source: Internet
Author: User
{Code ...}
When building a php Chinese verification code, the original idea is to split a paragraph into texts to form a text array, traverse four times, and randomly generate four verification texts, it is then painted on the image. However, when 'str _ split ($ str, 3) 'is used to separate the image, sometimes one less text is replaced with 'explode ("", $ str) 'or occasionally missing words... The main code is as follows: '$ fontFace = 'aa. ttf'; $ str = "The Grand gate is the main guide brand of China, and the industry association will re-promote the support and sales of the sponsors "; $ 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 (); $ y = mt_rand (); imagettftext ($ img, 15, mt_rand ), $ x, $ y, $ fontColor, $ fontFace, $ cn );} 'I think it is a text encoding problem. When the number of bytes is set to 4, some text can be displayed. But I don't know how to solve it. Please help. Thank you!

Reply content:
When building a php Chinese verification code, the original idea is to split a paragraph into texts to form a text array, traverse four times, and randomly generate four verification texts, it is then painted on the image. However, when 'str _ split ($ str, 3) 'is used to separate the image, sometimes one less text is replaced with 'explode ("", $ str) 'or occasionally missing words... The main code is as follows: '$ fontFace = 'aa. ttf'; $ str = "The Grand gate is the main guide brand of China, and the industry association will re-promote the support and sales of the sponsors "; $ 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 (); $ y = mt_rand (); imagettftext ($ img, 15, mt_rand ), $ x, $ y, $ fontColor, $ fontFace, $ cn );} 'I think it is a text encoding problem. When the number of bytes is set to 4, some text can be displayed. But I don't know how to solve it. Please help. Thank you!


Two vertices. The first is $ str separated by spaces, and the second is-1, not + 1, when a random index is generated.
Index can be used to obtain a random value in an array.array_rand()Function generation
The changes are as follows:

$ FontFace = 'aa. ttf'; $ str = "dashboard is a key recommendation product for Chinese leading brands and industry associations"; $ 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 (); $ y = mt_rand (); imagettftext ($ img, 15, mt_rand ), $ x, $ y, $ fontColor, $ fontFace, $ cn );}

Use the mb extended function to process strings and solve the encoding problem. What is the space used?

If $ str contains English and Chinese characters, use this delimiter.$strDb=preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);

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.