JChilds Posted October 6, 2011 Share Posted October 6, 2011 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 https://forums.phpfreaks.com/topic/248548-get-values-from-string-possibly-json-array/ Share on other sites More sharing options...
AyKay47 Posted October 6, 2011 Share Posted October 6, 2011 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 https://forums.phpfreaks.com/topic/248548-get-values-from-string-possibly-json-array/#findComment-1276434 Share on other sites More sharing options...
JChilds Posted October 6, 2011 Author Share Posted October 6, 2011 The string will always be different(in both cases), but the format the same . Link to comment https://forums.phpfreaks.com/topic/248548-get-values-from-string-possibly-json-array/#findComment-1276437 Share on other sites More sharing options...
AyKay47 Posted October 6, 2011 Share Posted October 6, 2011 then those two snippets will work for you Link to comment https://forums.phpfreaks.com/topic/248548-get-values-from-string-possibly-json-array/#findComment-1276440 Share on other sites More sharing options...
JChilds Posted October 6, 2011 Author Share Posted October 6, 2011 Thankyou very much Link to comment https://forums.phpfreaks.com/topic/248548-get-values-from-string-possibly-json-array/#findComment-1276454 Share on other sites More sharing options...
AyKay47 Posted October 6, 2011 Share Posted October 6, 2011 np, please mark as solved Link to comment https://forums.phpfreaks.com/topic/248548-get-values-from-string-possibly-json-array/#findComment-1276456 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.