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 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; 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"; 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 ?> Link to comment https://forums.phpfreaks.com/topic/186301-parse-string/#findComment-983870 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.