PHP获取汉字笔画数功能【测试可用】
公司小程序需要这么个功能,网上找了方法自己记录一下,哈哈哈
代码
<?php
// ini_set("error_reporting","E_ALL & ~E_NOTICE");
class Stroke
{
var $data;
var $dataIndex;
var $dataSpecial;
var $tone;
var $head;
var $format;
var $foot;
function setTone($tone)
{
$this->tone=$tone;
}
function setFormat($head, $format, $foot)
{
$this->head=$head;
$this->format=$format;
$this->foot=$foot;
}
function find($str, $isFirst=true)
{
for ($i=0;$i<36;$i++){
for ($j=0;$j<5;$j++){
$index=@strstr($this->data[$i][$j], $str);
if (!($index===false)&&(intval($index)%2==0))
return ($i+1);//($i+1).($isFirst?$this->dataIndex[$j]:"")
}
}
return " ";
}
function render($str, $ascii=false)
{
if ($ascii)
return str_replace("%1", $str, str_replace("%2", $str, $this->format));
$bihua=strstr($this->dataSpecial, $str." ")?$str:$this->find($str." ", $this->tone);
return str_replace("%1", $str, str_replace("%2", $bihua, $this->format));
}
function parse($str)
{
$output="";
$length=strlen($str);
$output.=$this->head;
for ($i=0;$i<$length;++$i){
if ($i==$length-1){
$output.=$this->render($str[$i], true);
break;
}
$code1=ord($str[$i]);
$code2=ord($str[$i+1]);
if ($code1==0x0D&&$code2==0x0A){
$output.=$this->foot;
$output.=$this->head;
++$i;
}else if ($code1>=0x81&&$code1<=0xFE&&$code2>=0x40&&$code2<=0xFE&&$code2!=0x7F){
$output.=$this->render($str[$i].$str[$i+1]);
++$i;
}
else
$output.=$this->render($str[$i], true);
}
$output.=$this->foot;
return $output;
}
function __construct()
{
$this->tone=true;
$this->head="";
$this->format="";
$this->foot="%2%1";
$this->dataIndex=array("一", "丨", "丿", "丶", "乛");
$this->dataSpecial="! " # ¥ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | }  ̄ 、 。 · ˉ ˇ ¨ 〃 々 — ~ ‖ … ‘ ' “ ” 〔 〕 〈 〉 《 》 「 」 『 』 〖 〗 【 】 ± × ÷ ∶ ∧ ∨ ∑ ∏ ∪ ∩ ∈ ∷ √ ⊥ ∥ ∠ ⌒ ⊙ ∫ ∮ ≡ ≌ ≈ ∽ ∝ ≠ ≮ ≯ ≤ ≥ ∞ ∵ ∴ ♂ ♀ ° ′ ″ ℃ $ ¤ ¢ £ ‰ § № ☆ ★ ○ ● ◎ ◇ ◆ □ ■ △ ▲ ※ → ← ↑ ↓ 〓 〡 〢 〣 〤 〥 〦 〧 〨 〩 ㊣ ㎎ ㎏ ㎜ ㎝ ㎞ ㎡ ㏄ ㏎ ㏑ ㏒ ㏕ ︰ ¬ ¦ ℡ ㈱ ‐ ー ゛ ゜ ヽ ヾ 〆 ゝ ゞ ﹉ ﹊ ﹋ ﹌ ﹍ ﹎ ﹏ ﹐ ﹑ ﹒ ﹔ ﹕ ﹖ ﹗ ﹙ ﹚ ﹛ ﹜ ﹝ ﹞ ﹟ ﹠ ﹡ ﹢ ﹣ ﹤ ﹥ ﹦ ﹨ ﹩ ﹪ ﹫ 〇 ─ ━ │ ┃ ┄ ┅ ┆ ┇ ┈ ┉ ┊ ┋ ┌ ┍ ┎ ┏ ┐ ┑ ┒ ┓ └ ┕ ┖ ┗ ┘ ┙ ┚ ┛ ├ ┝ ┞ ┟ ┠ ┡ ┢ ┣ ┤ ┥ ┦ ┧ ┨ ┩ ┪ ┫ ┬ ┭ ┮ ┯ ┰ ┱ ┲ ┳ ┴ ┵ ┶ ┷ ┸ ┹ ┺ ┻ ┼ ┽ ┾ ┿ ╀ ╁ ╂ ╃ ╄ ╅ ╆ ╇ ╈ ╉ ╊ ╋ ";
$this->data[0]