Get between square brackets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    Get between square brackets

    Code:
    $special_chr = '\w\~\`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\\\{\}\¦\:\;\"\'\?\/\>\.\<\, ';
    
    $str = '[name=mickel[]][last=sdaddads]';
    
    preg_match_all("/\[([$special_chr]+=[$special_chr]*)\]/D", $str, $match);
    how can i get name mickel[] with this pattern, it only gets last.
  • smartic
    New Member
    • May 2007
    • 150

    #2
    any help please ?

    Comment

    • JKing
      Recognized Expert Top Contributor
      • Jun 2007
      • 1206

      #3
      I am not all that great with regular expressions.

      What about after replacing all special characters searching for the [] and switching that to a special character.

      Then you can run a regular expression to extract the two matches between the square brackets.

      After that replace your special character with [].

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Nesting is tough to deal with. Generally, you'd need more than a single regex call.

        Comment

        Working...