所以,我有一个逗号分隔的字符串,我希望在x逗号计数后分成两个字符串。
所以,如果我的字符串如下:
temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,temp9,temp10
我希望在计算4个逗号之后将其分成两个字符串,我希望:
$string1 = 'temp1, temp2, temp3, temp4, ';
$string2 = 'temp5, temp6, temp7, temp8, temp9, temp10';
如何在PHP中实现这一目标?
答案 0 :(得分:3)
只是为了好玩。 {4}
可以更改或替换为变量:
preg_match('/([^,]+,){4}/', $string, $matches);
$string1 = $matches[0];
$string2 = str_replace($string1, '', $string);
答案 1 :(得分:1)
有很多方法可以做到这一点 - 一种方法是爆炸字符串然后内爆这个数组的切片:
$x = 4;
$delim = ', ';
$str = 'temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10';
$parts = explode($delim,$str);
$string1 = implode($delim,array_slice($parts,0,$x)) . $delim;
$string2 = implode($delim,array_slice($parts,$x));
对我来说,这是完成工作最简洁,最易读的方法,不需要循环。
答案 2 :(得分:0)
另一种方法是迭代每个char:
$string = "temp1, temp2, temp3, temp4, temp5, temp6, temp7";
$commacount = 0;
$stringBefore = "";
$stringAfter = "";
$splitHasHappened = false;
foreach (str_split($string) as $char) {
if ($splitHasHappened) {
$stringAfter .= $char;
}
else {
$stringBefore .= $char;
}
if ($char == ",") {
$commacount++;
}
if ($commacount == 4) {
$splitHasHappened = true;
}
}
echo "Before: $stringBefore - After $stringAfter"
答案 3 :(得分:0)
你可以创建一个带有2个参数的简单函数(逗号分隔的字符串和要拆分的索引。结果将是一个带有2个键的数字索引数组:0和1. $ array [0]拥有第一个part和$ array 1包含第二部分。下面是函数:
<?php
$strings = "temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10";
function splitAtIndex($commaString, $index){
$arrBlended = array();
$arrStrings = preg_split("#,\s?#", $commaString);
if(!empty($arrStrings)){
$partB = array_splice($arrStrings, $index);
$partA = $arrStrings;
$arrBlended[] = implode(", ", $partA) . ", ";
$arrBlended[] = implode(", ", $partB);
}
return $arrBlended;
}
$arrSplitStrings = splitAtIndex($strings, 4);
$string1 = $arrSplitStrings[0]; //<== 'temp1, temp2, temp3, temp4, '
$string2 = $arrSplitStrings[1]; //<== 'temp5, temp6, temp7, temp8, temp9, temp10'
var_dump($arrSplitStrings);
// DUMPS
array (size=2)
0 => string 'temp1, temp2, temp3, temp4, ' (length=28)
1 => string 'temp5, temp6, temp7, temp8, temp9, temp10' (length=41)
测试并确认结果HERE:
答案 4 :(得分:0)
你可以将它爆炸成数组
$array = explode(",",$string);
for($i=0;$i<4;$i++){
$string1[$i]=$array[$i];
}
$string1 = implode(",",$string1);
for($i=4;$i<count($array);$i++){
$string2[$i]=$array[$i];
}
$string2 = implode(",",$string2);
答案 5 :(得分:0)
我不喜欢使用循环。您实际上需要检查/验证输入:
<?php
$count = 4;
$str = 'temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10';
$match = preg_match('/^([^,]+,\s?){' . $count . '}/', $str, $matches) ? $matches[0] : false;
if ($match) {
$position = strlen($match);
$str1 = substr($str, 0, $position);
$str2 = substr($str, $position);
}
答案 6 :(得分:0)
使用array_slice
,array_filter
,substr
和$str = "temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10";
$comma_number = 4;
// finding the position of 4th comma
$pos = key(array_slice(array_filter(str_word_count($str, 2, ","), function($v){
return $v == ",";
}), $comma_number - 1, 1, true));
$string1 = substr($str, 0, $pos + 2);
$string2 = substr($str, $pos + 2);
var_dump($string1, $string2);
函数的解决方案:
string(28) "temp1, temp2, temp3, temp4, "
string(41) "temp5, temp6, temp7, temp8, temp9, temp10"
输出:
{{1}}
答案 7 :(得分:0)
这个Great For Me可以分割特定逗号的计数值并添加br标签
<?php
$string='temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10';
$delim = ',';
$x = 4;
$parts = explode($delim,$string);
$numberofloop=ceil(Count($parts)/$x);
for($i=0,$j=0;$i<$numberofloop;$i++){
echo implode($delim,array_slice($parts,$j,$x))."<br>";
$j=$j+$x;
}
?>