nackle2k10 Posted July 28, 2013 Share Posted July 28, 2013 Hi, I'm trying to turn input from textarea into an arrayhere is what the textarea input look like[input 1] [input 2] [input 3]I'm trying to split everything inside the square bracket "[]" into an array.here are my codes $option = array(); $regex = "#[\[^]]*#s"; $input = "[input 1] [input 2] [input 3]"; $option = preg_split($regex, $input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); the output is Array ( [0] => input 1] [1] => input 2] [2] => input 3] ) the closing bracket still there, So what am I missing in the regex pattern? Thanks Link to comment https://forums.phpfreaks.com/topic/280604-need-help-with-regular-expression/ Share on other sites More sharing options...
jazzman1 Posted July 28, 2013 Share Posted July 28, 2013 Use () to separate the regEx class. Try, $regex = "/\[(.*?)\]\s?/"; OR, $regex = "~\[(.*?)\]\s?~"; Link to comment https://forums.phpfreaks.com/topic/280604-need-help-with-regular-expression/#findComment-1442528 Share on other sites More sharing options...
.josh Posted July 29, 2013 Share Posted July 29, 2013 Near as I can tell, you're using preg_split in a funky way. Normally it's used for splitting with patterned delimiters, not for directly capturing stuff. IOW, near as I can tell, you were making the thing you wanted to match the delimiter and then using PREG_SPLIT_DELIM_CAPTURE to flip the roles of delimiter/split content, in order to effectively match for what you wanted. You can do it this way, but it's a funky ass-backwards way of doing it. Instead, when you should just be use the straightforward preg_match_all function instead. This is how you would normally do it: You use preg_match_all to match for your stuff: $option = array(); $regex = '~\[([^\]]+)\]~'; $input = "[input 1] [input 2] [input 3]"; preg_match_all($regex, $input, $matches); $option = $matches[1]; If you really want to go the preg_split route, normally you would split by the delimiter, which is "] [". I threw some alternation into the mix, along with the PREG_SPLIT_NO_EMPTY flag, as a means to effectively strip the leading "[" and trailing "]" but alternatively (and perhaps ideally) you could instead trim $input first, to get rid of them, so that the preg_split pattern would only be dealing with the delimiter. $option = array(); $regex = '~^\[|\]\s+\[|\]$~'; $input = "[input 1] [input 2] [input 3]"; $option = preg_split($regex, $input, -1, PREG_SPLIT_NO_EMPTY); Link to comment https://forums.phpfreaks.com/topic/280604-need-help-with-regular-expression/#findComment-1442549 Share on other sites More sharing options...
nackle2k10 Posted July 31, 2013 Author Share Posted July 31, 2013 @grumpy the reason why I don't use trim() to strip the "[" and "]" is the input are not handled by me, so user may use enter when inputing the option or not even use space at all. The solution I use is this pattern $regex = "#\[|\]\s+|[\]]#s"; $input = "[input 1] [input 2] [input 3]"; $option = preg_split($regex, $input, -1, PREG_SPLIT_NO_EMPTY); what do you guys think? Does this pattern is the best practice? Btw, what happen to this forum yesterday? I tried to visit it few times and got 404 error. Link to comment https://forums.phpfreaks.com/topic/280604-need-help-with-regular-expression/#findComment-1442860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.