PHP获取汉字笔画数功能

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]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值