opencombatclan Posted December 11, 2009 Share Posted December 11, 2009 Hello everyone, here I am once again. This time I need to know something about reading values from a predefined template. Lets say I have got the following template [TEST:1531222 12.11.2009] 35f8519d1980ccfc771A0e5986af5e06 "name" "ip" "type" And I need the following variables from this string. $date $hash $name $ip $type Some more info about the string: the first part (TEST) is always the same then a number (which can be short or long) follows then a date then a hash then the name (which can be short or long) then a ip (which can be short or long) then a type (which can be short or long) An empty string looks like: [TEST: ] "" "" "" I hope someone can help me Maybe a regular expression would do the trick? Quote Link to comment https://forums.phpfreaks.com/topic/184773-simple-way-to-get-values-from-a-predefined-template/ Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 It's difficult from your example to work out exactly what's there and what's simply to get your point across, but it seems like everything is there (including brackets and quotes), so that's what I'm going to go with. $input = '[TEST:1531222 12.11.2009] 35f8519d1980ccfc771A0e5986af5e06 "name" "ip" "type"'; preg_match('#\[TEST:([0-9]*) ((?:[0-9]{2}\.){2}[0-9]{4})\] ([a-z0-9]{32}) "([^"]*)" "([^"]*)" "([^"]*)"#i', $input, $out); echo '<pre>'; print_r($out); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/184773-simple-way-to-get-values-from-a-predefined-template/#findComment-975497 Share on other sites More sharing options...
opencombatclan Posted December 11, 2009 Author Share Posted December 11, 2009 Than you very much!!! :D This is exactly what i needed! Quote Link to comment https://forums.phpfreaks.com/topic/184773-simple-way-to-get-values-from-a-predefined-template/#findComment-975534 Share on other sites More sharing options...
cags Posted December 11, 2009 Share Posted December 11, 2009 You're welcome. By the way, thanks for actually stating your example clearly, specifying parameters for each match, and giving an example input string. It's not often we get that and it's so much easier to help people when they do. You could if you wanted narrow it down more or actually validate dates (within certain tolerances) or the IP the worlds your oyster, but I'm assuming none of that is really a requirement. EDIT: Oh and if you consider this solved, click the 'Mark Solved' button (bottom left of threads you start. Quote Link to comment https://forums.phpfreaks.com/topic/184773-simple-way-to-get-values-from-a-predefined-template/#findComment-975537 Share on other sites More sharing options...
nrg_alpha Posted December 13, 2009 Share Posted December 13, 2009 You're welcome. By the way, thanks for actually stating your example clearly, specifying parameters for each match, and giving an example input string. It's not often we get that and it's so much easier to help people when they do. ... Quote Link to comment https://forums.phpfreaks.com/topic/184773-simple-way-to-get-values-from-a-predefined-template/#findComment-976349 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.