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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.