1

I have a string is below,

$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";"

Now I would like to convert this string as a sub-string to be an array element is as below,

$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",
];

After five commas counted in the $string, a substring will be converted as an array element.
How can I do it using PHP?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Optimus Prime
  • 308
  • 6
  • 22

5 Answers5

5

You could use the array_chunk method to solve this

<?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

DarkBee
  • 16,592
  • 6
  • 46
  • 58
3

Based on the input string in the question, you can also use preg_split, splitting on a comma followed by a newline:

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

Output:

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

Update

Based on the comments, I now understand there isn't intended to be any whitespace in the string. In this case, you can still use preg_split with a more complex regex, and using the flags PREG_SPLIT_DELIM_CAPTURE to capture the contents of the regex and PREG_SPLIT_NO_EMPTY to remove empty strings from the result:

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

Output is the same. Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • No Nick. It's a one-line string. No whitespace. Context is clearly after 5th comma. – Rahul Aug 30 '19 at 06:29
  • @Rahul in the question the string has whitespace. That's what I'm answering (as did one of the others). – Nick Aug 30 '19 at 06:30
  • 1
    @Nick I think the OP broke out the string to multiple lines to make it more readable, which inadvertently changed the question, but +1 to you anyway. – Tim Biegeleisen Aug 30 '19 at 06:31
  • @Nick Tim is right! Pardon mate! To make it more readable I did editing :p. – Rahul Aug 30 '19 at 06:34
  • @Rahul You should unedit the question as there are other answers that rely on the white space. I've updated my answer with a more robust whitespace independent solution. – Nick Aug 30 '19 at 06:36
  • @Nick +1 from me too. I did what you say, mate! \M/ here's [another regex](https://stackoverflow.com/questions/57721669/regex-expression-for-comma-or-space-separated-string) for you! – Rahul Aug 30 '19 at 06:39
1

We can try using preg_match_all here with the following regex:

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

This will match a term having fives commas, but won't capture the final fifth comma as the first capture group.

$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]);

This prints:

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
)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

I'm not a PHP expert, but its just a split and reform in any language.

<?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

?>

Will yield

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
)
Kris
  • 8,680
  • 4
  • 39
  • 67
0

I do not see any newlines in the shown string. And is the number of commas always the same for the separation? What is in the original: "heading: Quick, Link" or "heading: Quick Link"? The expected array shows me that every array element should start with a "div". Why not use "div" or "div-item" to separate? Under these conditions do also do that:

$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);
jspit
  • 7,276
  • 1
  • 9
  • 17