For example, I want to turn $str = 'adfdf' into an array like ['a', 'd', 'f', 'd', 'f']
曾经蜡笔没有小新2017-06-21 10:13:28
I have done string comparison before using the lcs algorithm, which involves breaking up strings into arrays. Due to Chinese issues, it is not possible to use str_split directly. You need to use preg_split
你可以封装一个公共函数
function mb_str_split($str)
{
return preg_split('/(?<!^)(?!$)/u' , $str);
}
我想大声告诉你2017-06-21 10:13:28
$string = '中国共产党万岁!aaaaa';
var_dump(preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY));
/*
array (size=13)
0 => string '中' (length=3)
1 => string '国' (length=3)
2 => string '共' (length=3)
3 => string '产' (length=3)
4 => string '党' (length=3)
5 => string '万' (length=3)
6 => string '岁' (length=3)
7 => string '!' (length=3)
8 => string 'a' (length=1)
9 => string 'a' (length=1)
10 => string 'a' (length=1)
11 => string 'a' (length=1)
12 => string 'a' (length=1)
*/
扔个三星炸死你2017-06-21 10:13:28
The string itself can be used as an array
$str = "abcdef";
You output $str[0].$str[1].$str[2];
You will find that it is actually $str
array(" a","b","c","d","e","f");
学习ing2017-06-21 10:13:28
$str = 'adfdf';
$arr = [];
for($i=0;$i<strlen($str);$i++) {
$arr[$i] = $str{$i};
}
var_dump($arr);
$str = 'adfdf';
preg_match_all('/(\w{1})/', $str, $matches);
var_dump($matches[0]);
phpcn_u15822017-06-21 10:13:28
$str = 'adfdf';
$temp = array();
for ($i = 0; $i < strlen($str); $i++) {
array_push($temp, $str{$i});
}
var_dump($temp);
巴扎黑2017-06-21 10:13:28
$str = 'adfdf';
$len = mb_strlen($str);
$arr = [];
if(!isset($str[0])) {
exit('输入合理字符串!');
}
$rb = function ($index = 0) use (&$rb, &$arr, $str, $len) {
array_push($arr, mb_substr($str, $index, 1,'UTF-8'));
$index++;
if ($index == $len) return $arr;
$rb($index);
};
$rb();
header("Content-type: text/html; charset=UTF-8");
var_dump($arr);
代言2017-06-21 10:13:28
<?php
$str = 'adfdf';
$arr = array();
for($i=0;$i<strlen($str);$i++) {
$arr[$i] = $str[$i];
}
print_r($arr);
?>