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. Quote Link to comment 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); Quote Link to comment 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 . Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
JChilds Posted October 6, 2011 Author Share Posted October 6, 2011 Thankyou very much Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted October 6, 2011 Share Posted October 6, 2011 np, please mark as solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.