refiking Posted October 14, 2010 Share Posted October 14, 2010 I have a variable that has something like: datetime_system=>2010-10-14 02:07:07{~-SePaRaT0R-~}username_system=>test (16){~-SePaRaT0R-~}ip_system=>79.354.76.127 All I want is $userid = 16. How do I go about this?[/code] Quote Link to comment https://forums.phpfreaks.com/topic/215841-how-to-parse-a-variable/ Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 That looks very close to an array. Is that one string in a variable or what? Quote Link to comment https://forums.phpfreaks.com/topic/215841-how-to-parse-a-variable/#findComment-1122148 Share on other sites More sharing options...
premiso Posted October 14, 2010 Share Posted October 14, 2010 $text = 'datetime_system=>2010-10-14 02:07:07{~-SePaRaT0R-~}username_system=>test (16){~-SePaRaT0R-~}ip_system=>79.354.76.127'; preg_match('~.*username_system=>.* \(([0-9]+)\).*~i', $text, $matches); echo $matches[1]; Should do what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/215841-how-to-parse-a-variable/#findComment-1122151 Share on other sites More sharing options...
refiking Posted October 17, 2010 Author Share Posted October 17, 2010 That looks very close to an array. Is that one string in a variable or what? Yes it is very close to an array. How can I convert it to an array and then parse out the different values? Quote Link to comment https://forums.phpfreaks.com/topic/215841-how-to-parse-a-variable/#findComment-1122986 Share on other sites More sharing options...
.josh Posted October 17, 2010 Share Posted October 17, 2010 well if you have control over what the format of $text is and you need it to be a string, I would suggest you serialize the data (array) into a string for $text and then unserialize $text when you need it, which will restore it to the original array. If you do not have access to change the format of the $text string, assuming the format is regular (looks like what you have up there, but all the time), you could explode at the delimiter loop through and explode at => for the key => value pairs, basically simulating unserialize(). Though you may run into issues if values happen to be your delimiter or => as well... Otherwise the safest way to do it would probably be to do what premiso suggested. Quote Link to comment https://forums.phpfreaks.com/topic/215841-how-to-parse-a-variable/#findComment-1123036 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.