{{ skargor.blog }}

Posts Tagged ‘PHP’

PHP Simple Digital Captcha

Captcha.php文件: <?php session_start(); Header("Content-type: image/PNG"); $im = imagecreate(44,18); //captcha size $back = ImageColorAllocate($im, 245,245,245); imagefill($im,0,0,$back); //background color srand((double)microtime()*1000000); //genereate 4 digital numbers for($i=0;$i<4;$i++){ $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); $authnum=rand(1,9); $vcodes.=$authnum; imagestring($im, 5, 2+$i*10, 1, $authnum, $font); } for($i=0;$i<100;$i++) { //make it hard to read $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im, rand()%70, rand()%30, $randcolor); } ImagePNG($im); ImageDestroy($im); $_SESSION['vcode'] = [...]

PHP keep whole word when substring

有時候要 substr 一段文字, 如果直接使用 substr 會出現單詞斷開的現象, 利用 word wrap 可以簡單的保證單詞不斷開 $long_str = ""; //你需要截取的文字 $str_limit = 150; // 你需要截取的文字長度 $break_sign = "<!– word break –>"; //用來區分段落分段, 可以用任何和你要截取的內容不衝突的字段 $str = substr($str=wordwrap($long_str, $str_limit, $break_sign), 0, strpos($str, $break_sign)); //截取 這樣就不會單詞斷開了. 先利用 wordwrap 把文字分段, 然後 strpos 定義斷開符號的位置, 然後截取斷開符號之前所有的文字.