php对UTF8字体串进行单字分割返回数组

在网上查了很多字符串分割方法,都无法正确对UTF8字符串进行正确分割返回单个字符的数组。经过对FTU8编码的分析写出了下面的方法对UTF8进行分割。本人测试可用。本方法只支持UTF8编码的,其它编码转自行转换成UT8再使用。

$tempaddtext="http://www.jishubu.net php对UTF8字体串进行单字分割返回数组";
//$tempaddtext=iconv("GBK","UTF-8",$tempaddtext); //字符编码转换,自行判定需要不需要
$cind = 0;
$arr_cont = array();
for ($i = 0; $i < strlen($tempaddtext); $i++) {
           if (strlen(substr($tempaddtext, $cind, 1)) > 0) {
               if (ord(substr($tempaddtext, $cind, 1)) < 192) {
                   if (substr($tempaddtext, $cind, 1) != " ") {
                       array_push($arr_cont, substr($tempaddtext, $cind, 1));
                   }
                   $cind++;
               } elseif(ord(substr($tempaddtext, $cind, 1)) < 224) {
                   array_push($arr_cont, substr($tempaddtext, $cind, 2));
                   $cind+=2;
               } else {
                   array_push($arr_cont, substr($tempaddtext, $cind, 3));
                   $cind+=3;
               }
           }
       }

        print_r($arr_cont);

  返回结果:

Array ( [0] => h [1] => t [2] => t [3] => p [4] => : [5] => / [6] => / [7] => w [8] => w [9] => w [10] => . [11] => j [12] => i [13] => s [14] => h [15] => u [16] => b [17] => u [18] => . [19] => n [20] => e [21] => t [22] => p [23] => h [24] => p [25] => 对 [26] => U [27] => T [28] => F [29] => 8 [30] => 字 [31] => 体 [32] => 串 [33] => 进 [34] => 行 [35] => 单 [36] => 字 [37] => 分 [38] => 割 [39] => 返 [40] => 回 [41] => 数 [42] => 组 )

  

php对UTF8字体串进行单字分割返回数组,布布扣,bubuko.com

时间: 2024-08-25 15:05:09

php对UTF8字体串进行单字分割返回数组的相关文章

c#调用dll接口传递utf-8字串方法

1. 起源: VCU10之视频下载模块,采用纯python实现,c++代码调用pythonrun.h配置python运行环境启动python模块,以使界面UI能够使用其中功能. 棘手问题!用去一天时间反复打印日志,验证所传字串区别,以期望发现问题定位问题,直至下班前始有灵感. 验证发现,非中文字符可以正常下载,中文字符下载解析失败,当时即想到可能是字串不统一所致,就在python代码中做字串转换处理,均不奏效. 原接口封装代码如下: [DllImport("VideoDownloader&quo

JS中,split()用法(将字符串按指定符号分割成数组)

<!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> <title></title> </head> <body> </body> <script type="text/javascript"> //在Javascript脚本中,将字符串按指定符号进行分割成数组用split()的方法 //

将中文字符串分割为数组 解决str_split中文乱码php

首先来介绍str_split()这个函数: 它的作用是将字符串分割为数组: 例如: $str='abcde';str_plite($str); 打印结果如下:Array(    [0] => a    [1] => b    [2] => c    [3] => d    [4] => e) 看似很好用的样子:但是作为中国程序员:不可避免的要和中文打交道: 这时候再用str_splite就会悲剧的发现乱码了::: 不要怕:preg_splite可以拯救这个问题: 当然是需要配

php把字符串指定字符分割成数组

<?php $str="1|2|3|4|5|"; $var=explode("|",$str); print_r($var); ?> $var=explode("|",$str);把$str按|进行分割php还有其他的把字符串指定字符分割成数组str_split(string,length)参数 描述 string 必需.规定要分割的字符串. length 可选.规定每个数组元素的长度.默认是 1. json_decode()这个函数

Bag标签之把字符串分割成数组,返回一个存放数组数据的书包(Bag)

把字符串分割成数组,返回一个存放数组数据的书包(Bag): 操作 <Bag id=test act=splitgap="/">a/b/c/d</Bag> 把字符串a/b/c/d数组,返回书包test,按关键字test0.test1.test2...取出分割后的值 输出分割后的值 <for end=0 [email protected]{test:getWidth}> <b>@{test:[email protected]{for:getS

利用split对一个字符串按逗号“”,“”分割成数组

Stirng attach_id = "a,da,dsa,rew,rew,dsa"; String[] arr=new String(attach_id).split("[\\,]"); //用split对一个字符串按逗号分割成数组 String str = Arrays.toString(arr); //str数组 原文地址:https://www.cnblogs.com/Darkqueen/p/12068115.html

MyEclipse2015各种好用的自定义配置(SVN, JDK, Maven, Tomcat, 自动补全, UTF-8, 字体等)

以MyEclipse2015为例,各种好用的配置如下.   安装SVN插件:1. SVN官网查看最新的版本:http://subclipse.tigris.org,进入“Download and Install”2. 选择一个版本的url,比如:http://subclipse.tigris.org/update_1.10.x3. MyEclipse: Help -> Install from Site... -> add...把第2步的url填入“Location”中,点击OK4. 勾选 S

MyEclipse各种好用的自定义配置(SVN, JDK, Maven, Tomcat, 自动补全, UTF-8, 字体等)

以MyEclipse2015为例,各种好用的配置如下. 安装SVN插件: 1. SVN官网查看最新的版本:http://subclipse.tigris.org,进入"Download and Install" 2. 选择一个版本的url,比如:http://subclipse.tigris.org/update_1.10.x 3. MyEclipse: Help -> Install from Site... -> add...把第2步的url填入"Locati

判断字体串为空

public class StringUtil { public static boolean isEmpty(String str){  if(str==null|| str.trim().length()==0){   return true;  }  return false; } public static boolean isNotEmpty(String str){    return !isEmpty(str); } public static boolean isBlank(St