使用 PHP 在字符串中计数五个逗号后将字符串转换为数组

Convert string to array after count five comma in the string using PHP

我下面有一个字符串,

$string = "div-item-0,4,maintype:menu| heading: Quick Link|  isactive:1,0,0, div-item-1,4,maintype:text| heading:Image|  isactive:1,4,0, div-item-2,4,maintype:social| heading:Social|  isactive:1,8,0";"

现在我想把这个字符串转换为一个子字符串成为一个数组元素,如下所示,

$array = [
    "div-item-0,4,maintype:menu| heading: Quick, Link| isactive:1,0,0",
    "div-item-1,4,maintype:text| heading:Image| isactive:1,4,0",
    "div-item-2,4,maintype:social| heading:Social| isactive:1,8,0",
];

$string中计算五个逗号后,子字符串将被转换为数组元素。
如何使用 PHP?

我们可以尝试在此处使用 preg_match_all 和以下正则表达式:

((?:[^,]*,){4}[^,]+)(?:,|$)

这将匹配包含五个逗号的术语,但不会将最后的第五个逗号捕获为第一个捕获组。

$string = "
    div-item-0,4,maintype:menu| heading: Quick Link| isactive:1,0,0,  
    div-item-1,4,maintype:text| heading:Image|  isactive:1,4,0,  
    div-item-2,4,maintype:social| heading:Social|  isactive:1,8,0";

preg_match_all("/((?:[^,]*,){4}[^,]+)(?:,|$)/", $string, $matches);
print_r($matches[1]);

这会打印:

Array
(
    [0] => div-item-0,4,maintype:menu| heading: Quick Link| isactive:1,0,0
    [1] => div-item-1,4,maintype:text| heading:Image|  isactive:1,4,0
    [2] => div-item-2,4,maintype:social| heading:Social|  isactive:1,8,0
)

你可以使用array_chunk方法来解决这个问题

<?php

$string = "
div-item-0,4,maintype:menu| heading: Quick Link|  isactive:1,0,0,  
div-item-1,4,maintype:text| heading:Image|  isactive:1,4,0,  
div-item-2,4,maintype:social| heading:Social|  isactive:1,8,0";


$temp = explode(',', $string); // just create one big array
$temp = array_chunk($temp, 5); // group the array per 5 parts
foreach($temp as &$value) $value = trim(implode(',', $value)); // recombine to one string

var_dump($temp);

demo

我不是PHP专家,但它只是任何语言的分裂和改革。

<?php

$string = "div-item-0,4,maintype:menu| heading: Quick Link|  
isactive:1,0,0,div-item-1,4,maintype:text| heading:Image|  
isactive:1,4,0,div-item-2,4,maintype:social| heading:Social|  
isactive:1,8,0";
$items = explode("div-item",$string);
$target = array();
foreach( $items as $item){
    if($item !== '')
        array_push($target,"div-item".$item);
}

print_r($target); //Your array

?>

会产生

Array
(
    [0] => div-item-0,4,maintype:menu| heading: Quick Link|  
isactive:1,0,0,
    [1] => div-item-1,4,maintype:text| heading:Image|  
isactive:1,4,0,
    [2] => div-item-2,4,maintype:social| heading:Social|  
isactive:1,8,0
)

根据问题中输入的字符串,也可以使用preg_split,以逗号分隔,后跟换行符:

$array = preg_split('/,\s+/', trim($string));
print_r($array);

输出:

Array
(
    [0] => div-item-0,4,maintype:menu| heading: Quick Link|  isactive:1,0,0
    [1] => div-item-1,4,maintype:text| heading:Image|  isactive:1,4,0
    [2] => div-item-2,4,maintype:social| heading:Social|  isactive:1,8,0
)

Demo on 3v4l.org

更新

根据评论,我现在明白字符串中不应有任何空格。在这种情况下,您仍然可以将 preg_split 与更复杂的正则表达式一起使用,并使用标志 PREG_SPLIT_DELIM_CAPTURE 来捕获正则表达式的内容,并使用 PREG_SPLIT_NO_EMPTY 从结果中删除空字符串:

$array = preg_split('/((?:[^,]+,){4}[^,]+),\s*/', trim($string), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($array);

输出相同。 Demo on 3v4l.org

我在显示的字符串中没有看到任何换行符。 分隔符的逗号数量是否始终相同? 原文是什么:"heading: Quick, Link" 或 "heading: Quick Link"? 预期的数组告诉我每个数组元素都应该以 "div" 开头。为什么不用"div"或"div-item"来分隔呢? 在这些条件下也这样做:

$string = "div-item-0,4,maintype:menu| heading: Quick Link|  isactive:1,0,0, div-item-1,4,maintype:text| heading:Image|  isactive:1,4,0, div-item-2,4,maintype:social| heading:Social|  isactive:1,8,0";

$array = preg_split('~(?=div)~', $string,-1, PREG_SPLIT_NO_EMPTY);