Jump to content

Get values from string, possibly json array?


JChilds

Recommended Posts

Hi guys, I have been playing with regex, but it and I really do not get on apparently.

 

How would I go about matching '1234' in this expression:  (123|789,1234)

 

And, How would I get this as an array in php (im not sure if regex is the correct thing to use here)

' _x[5,1,0,0,0,0,0,0,1,0,1,4,4,9,10,5]' (the values within the [] obviously.

1. if that string that you provided is going to be the same pattern every time... you can use this

 

$string = "(123|789,1234)";
$pattern = "~\d{4}~";
preg_match($pattern,$string,$matches);
foreach($matches as $match){
	print $match."<br />";
}

 

2. you could combine regex with explode here..

 

$string = " _x[5,1,0,0,0,0,0,0,1,0,1,4,4,9,10,5]";
$pattern = "~.*\[([\d,]+)\]~";
preg_match($pattern,$string,$matches);
$array_string = $matches[1];
$new_arr = explode(",",$array_string);
print_r($new_arr);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.