SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help With Regular Expression

    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

    PHP Code:
    $option = array();
    $regex "#[\[^]]*#s";
    $input "[input 1] [input 2] [input 3]";

    $option preg_split($regex$input, -1PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE ); 
    the output is

    PHP Code:
    Array
    (
        [
    0] => input 1
        [
    1] => input 2
        [
    2] => input 3]

    the closing bracket still there, So what am I missing the regex pattern? Thanks

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,287
    Mentioned
    182 Post(s)
    Tagged
    4 Thread(s)
    I think you can avoid RegExp all together.
    Use explode instead.
    You should trim leading and trailing square brackets first with:
    PHP Code:
    $option explode("] ["trim($input"[]")); 
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a good way to create the array but since the input is inputted using texarea I cannot take control of the input it may be have enter or space between each bracket [] [] are there any solutions?

  4. #4
    SitePoint Member
    Join Date
    Jul 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I have solve this by using this regex
    Code:
    #\[|\]\s+|[\]]#s
    thanks


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •