I'm having way too much trouble with this simple problem: split a string into an array of 2-character values, i.e.
我对这个简单的问题有太多的麻烦:将一个字符串拆分成一个包含2个字符值的数组,即
$string = 'abcdefgh';
// With the correct regex, should return ['ab','cd','ef','gh'];
$array = preg_split("/?????/",$string);
What's the darn regex?
什么是darn正则表达式?
3 个解决方案
#2
1
Hint: If you split ON the characters, you end up with an array of 4 elements that are blank
提示:如果将字符拆分为ON,则最终会得到一个包含4个空白元素的数组
eg.
/../i
I don't think the preg_split is what you want, perhaps preg_match_all
? eg.
我不认为preg_split是你想要的,也许是preg_match_all?例如。
$cnt = preg_match_all('/../i', $string, $matches);
$ cnt = preg_match_all('/../ i',$ string,$ matches);
#3
0
/.{1,2}/
perhaps? Accept unlimited two character pairs, accept one character if necessary.
/.{1,2}/也许?接受无限制的两个字符对,必要时接受一个字符。
#1
#2
1
Hint: If you split ON the characters, you end up with an array of 4 elements that are blank
提示:如果将字符拆分为ON,则最终会得到一个包含4个空白元素的数组
eg.
/../i
I don't think the preg_split is what you want, perhaps preg_match_all
? eg.
我不认为preg_split是你想要的,也许是preg_match_all?例如。
$cnt = preg_match_all('/../i', $string, $matches);
$ cnt = preg_match_all('/../ i',$ string,$ matches);
#3
0
/.{1,2}/
perhaps? Accept unlimited two character pairs, accept one character if necessary.
/.{1,2}/也许?接受无限制的两个字符对,必要时接受一个字符。