Jump to content

Need Help With Regular Expression


nackle2k10

Recommended Posts

Hi, I'm trying to turn input from textarea into an array

here 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
Share on other sites

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
Share on other sites

@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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.