我想拆分数组元素的字符串。我必须将它拆分为第五个字符,但如果它位于单词的中间,则将其拆分为最近的空格。像这样:
// I have array like these
$array={"How are you ?","I am fine."};
//I want output like if we take length = 5.
$array1 = {"How are","you ?","I am fine."}; // array1[0] =how a should be array1[0] = how are .
答案 0 :(得分:0)
查找距离第五个字符最近的空格。 然后从0返回第一部分直到该字符并将其放入新数组。 然后将第二个块(直到结束的那个)放入同一个数组中。
foreach ($array as $val) {
$strpos = strpos($val, " ", 5);
$array1[] = substr($val, 0, $strpos);
$array1[] = substr($val, $strpos+1, strlen($val));
}