预览模式: 普通 | 列表

php获取中文首字母

汉字、英文、数字均适用。

PHP代码
  1. function getfirstchar($s0){   
  2. $s=iconv("UTF-8","gb2312"$s0);   
  3. $asc=ord($s{0})*256+ord($s{1})-65536;   
  4. if($asc>=-20319 and $asc<=-20284)return "A";   
  5. if($asc>=-20283 and $asc<=-19776)return "B";  
  6. if($asc>=-19775 and $asc<=-19219)return "C";   
  7. if($asc>=-19218 and $asc<=-18711)return "D";   
  8. if($asc>=-18710 and $asc<=-18527)return "E";   
  9. if($asc>=-18526 and $asc<=-18240)return "F";   
  10. if($asc>=-18239 and $asc<=-17923)return "G";   
  11. if($asc>=-17922 and $asc<=-17418)return "H";   
  12. if($asc>=-17417 and $asc<=-16475)return "J";   
  13. if($asc>=-16474 and $asc<=-16213)return "K";   
  14. if($asc>=-16212 and $asc<=-15641)return "L";   
  15. if($asc>=-15640 and $asc<=-15166)return "M";   
  16. if($asc>=-15165 and $asc<=-14923)return "N";   
  17. if($asc>=-14922 and $asc<=-14915)return "O";   
  18. if($asc>=-14914 and $asc<=-14631)return "P";   
  19. if($asc>=-14630 and $asc<=-14150)return "Q";   
  20. if($asc>=-14149 and $asc<=-14091)return "R";   
  21. if($asc>=-14090 and $asc<=-13319)return "S";   
  22. if($asc>=-13318 and $asc<=-12839)return "T";   
  23. if($asc>=-12838 and $asc<=-12557)return "W";   
  24. if($asc>=-12556 and $asc<=-11848)return "X";   
  25. if($asc>=-11847 and $asc<=-11056)return "Y";   
  26. if($asc>=-11055 and $asc<=-10247)return "Z";   
  27. return false;   
  28. }   

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 1946

直接上代码

PHP代码
  1. <?php  
  2. /** 
  3.  * 把一个汉字转为unicode的通用函数,不依赖任何库,和别的自定义函数,但有条件 
  4.  * 条件:本文件以及函数的输入参数应该用utf-8编码,不然要加函数转换 
  5.  * 其实亦可轻易编写反向转换的函数,甚至不局限于汉字,奇怪为什么PHP没有现成函数 
  6.  * @author xieye 
  7.  * 
  8.  * @param {string} $word 必须是一个汉字,或代表汉字的一个数组(用str_split切割过) 
  9.  * @return {string} 一个十进制unicode码,如4f60,代表汉字 “你” 
  10.  * 
  11.  * @example 
  12.     echo "你 ".getUnicodeFromOneUTF8("你"); 
  13.     echo "<br />"; 
  14.     echo "好 ".getUnicodeFromOneUTF8("好"); 
  15.     echo "<br />"; 
  16.     echo "你好 ".getUnicodeFromOneUTF8("你好"); 
  17.     echo "<br />"; 
  18.     echo "你好吗 ".getUnicodeFromOneUTF8("你好吗"); 
  19.     你 20320 
  20.     好 22909 
  21.     你好 251503099357000 
  22.     你好吗 4.21952182258E+21 
  23.  */  
  24. function getUnicodeFromOneUTF8($word) {  
  25.     //获取其字符的内部数组表示,所以本文件应用utf-8编码!  
  26.     if (is_array$word))  
  27.         $arr = $word;  
  28.     else  
  29.         $arr = str_split($word);  
  30.     //此时,$arr应类似array(228, 189, 160)  
  31.     //定义一个空字符串存储  
  32.     $bin_str = '';  
  33.     //转成数字再转成二进制字符串,最后联合起来。  
  34.     foreach ($arr as $value)  
  35.         $bin_str .= decbin(ord($value));  
  36.     //此时,$bin_str应类似111001001011110110100000,如果是汉字"你"  
  37.     //正则截取  
  38.     $bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3'$bin_str);  
  39.       
  40.     //此时, $bin_str应类似0100111101100000,如果是汉字"你"  
  41.     return bindec($bin_str);  
  42.     //返回类似20320, 汉字"你"  
  43.     //return dechex(bindec($bin_str));  
  44.     //如想返回十六进制4f60,用这句  
  45. }  
  46. echo "你 ".getUnicodeFromOneUTF8("你");  
  47. echo "<br />";  
  48. echo "好 ".getUnicodeFromOneUTF8("好");  
  49. echo "<br />";  
  50. echo "你好 ".getUnicodeFromOneUTF8("你好");  
  51. echo "<br />";  
  52. echo "你好吗 ".getUnicodeFromOneUTF8("你好吗");  
  53. exit;  
  54.   
  55. ?>  

来源网址:http://blog.csdn.net/nailwl/article/details/5520068

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2180

php随机中文字符串

中文验证码中需要用到的:

PHP代码
  1. function create_random_value() {  
  2.     $rand_value = '';  
  3.     $chinese = '的国在人了有中是年和大业不为发会工经上地市要个产这出行作生家以成到日民来我部对进多全建他公开们场展时理新方主企资实学报制政济用同于法高长现本月定化加动合品重关机分力自外者区能设后就等体下万元社过前面';  
  4.     for($i=1;$i<=4;$i++){  
  5.         $n=mt_rand(0,(mb_strlen($chinese,'utf-8')-1));  
  6.         $char=mb_substr($chinese$n, 1, 'utf-8');  
  7.         $rand_value .= $char;  
  8.     }  
  9.     return $rand_value;  
  10. }  

原创代码,转载请注明出处!

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2867

php图像函数imagettftext用法

功能:imagettftext — 用 TrueType 字体向图像写入文本

格式:array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )

参数:
image
图像资源。见 imagecreatetruecolor()。
size
字体大小。根据 GD 版本不同,应该以像素大小指定(GD1)或点大小(GD2)。
angle
角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。
x
由 x,y 所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。这和 imagestring() 不同,其 x,y 定义了第一个字符的左上角。例如 "top left" 为 0, 0。
y
Y 坐标。它设定了字体基线的位置,不是字符的最底端。
color
颜色索引。使用负的颜色索引值具有关闭防锯齿的效果。见 imagecolorallocate()。
fontfile
是想要使用的 TrueType 字体的路径。 根据 PHP 所使用的 GD 库的不同,当 fontfile 没有以 / 开头时则 .ttf 将被加到文件名之后并且会在库定义字体路径中尝试搜索该文件名。 当使用的 GD 库版本低于 2.0.18 时,一个空格字符 而不是分号将被用来作为不同字体文件的“路径分隔符”。不小心使用了此特性将会导致一条警告信息:Warning: Could not find/open font。对受影响的版本来说唯一解决方案就是将字体移动到不包含空格的路径中去。
很多情况下字体都放在脚本的同一个目录下。下面的小技巧可以减轻包含的问题。
 

PHP代码
  1. <?php  
  2. // Set the enviroment variable for GD  
  3. putenv('GDFONTPATH=' . realpath('.'));  
  4.   
  5. // Name the font to be used (note the lack of the .ttf extension)  
  6. $font = 'SomeFont';  
  7. ?>  

text
文本字符串。 可以包含十进制数字化字符表示(形式为:&#8364;)来访问字体中超过位置 127 的字符。UTF-8 编码的字符串可以直接传递。 如果字符串中使用的某个字符不被字体支持,一个空心矩形将替换该字符。
imagettftext() 返回一个含有 8 个单元的数组表示了文本外框的四个角,顺序为坐下角,右下角,右上角,左上角。这些点是相对于文本的而和角度无关,因此“左上角”指的是以水平方向看文字时其左上角。

查看更多...

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2836

先使用now()获得当前时间,与本机时间对比得到时间差,如15个小时。

然后用一下代码获得本地当前时间:

ASP/Visual Basic代码
  1. currenttime=dateadd("h",15,now())  
  2. currenttime=Year(currenttime)&"-"&Month(currenttime)&"-"&Day(currenttime)&" "&Hour(currenttime)&":"&minute(currenttime)  
  3. response.Write(currenttime)  

Tags: asp godaddy

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2022

 广告位

↑返回顶部↑