Explode string [ ]

Hellow.

i have a string output. "zoneAdd[1][home][group]"

I need.

$task = "ZoneAdd" $id = 1 $place = "home" $type = "group"

How to spit these guys? I need a PHP or javascript solution.

Answer

Here’s one way with Javascript. You can split the string using regex. It can output an array and from there you can loop and assign the strings to the variables you need

const str = "zoneAdd[1][home][group]"
const strArr = str.replace(/[[]']+/g,' ').trim().split(' ')

const task = strArr[0] 
const id = strArr[1] 
const place = strArr[2] 
const type = strArr[3]
console.log({strArr, task, id,place,type})