verdrm Posted December 25, 2009 Share Posted December 25, 2009 I need to parse the string below into three variables. How would I do that? john + 10/20/2008 - 10/30/2008 + and - are separators Quote Link to comment https://forums.phpfreaks.com/topic/186301-parse-string/ Share on other sites More sharing options...
Buddski Posted December 25, 2009 Share Posted December 25, 2009 $str = $var1 . ' + ' . $var2 . ' - ' . $var3; Quote Link to comment https://forums.phpfreaks.com/topic/186301-parse-string/#findComment-983867 Share on other sites More sharing options...
verdrm Posted December 25, 2009 Author Share Posted December 25, 2009 What are var1, var2, and var3? All I have right now is: $string = "john + 10/20/2008 - 10/30/2008"; Quote Link to comment https://forums.phpfreaks.com/topic/186301-parse-string/#findComment-983869 Share on other sites More sharing options...
Alex Posted December 25, 2009 Share Posted December 25, 2009 You can use preg_split to split a string by multiple delimiters. Example: <?php $str = "john + 10/20/2008 - 10/30/2008"; $var = array_map('trim', preg_split('~\+|\-~', $str)); echo $var[0]; // john echo $var[1]; // 10/20/2008 echo $var[2]; // 10/30/2008 ?> Quote Link to comment https://forums.phpfreaks.com/topic/186301-parse-string/#findComment-983870 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.