0

I have a preg_match problem. Here's the string

field[0][price][8]

And the regex

"/([\[]([a-zA-Z0-9\_]+)?[\]])+$/"

I want to check if the string ends with one ore more [ ] and extract all the values inside. When i put the $ flag, preg_match_all only gets the big and the last found groups. Something similar happens with preg_match.

Output:

array (size=3)
  0 => 
    array (size=1)
      0 => string '[0][price][8]' (length=13)
  1 => 
    array (size=1)
      0 => string '[8]' (length=3)
  2 => 
    array (size=1)
      0 => string '8' (length=1)

This is the desired result, that i get only if i omit the $ flag, which is not ok because i need to check if the string ends with the matched pattern:

array (size=3)
  0 => 
    array (size=3)
      0 => string '[0]' (length=3)
      1 => string '[price]' (length=7)
      2 => string '[8]' (length=3)
  1 => 
    array (size=3)
      0 => string '[0]' (length=3)
      1 => string '[price]' (length=7)
      2 => string '[8]' (length=3)
  2 => 
    array (size=3)
      0 => string '0' (length=1)
      1 => string 'price' (length=5)
      2 => string '8' (length=1)

Any ideas? Thanks a lot!

2
  • Just to be sure, would you like your example to only return 8 or 0 and price as well? Commented Aug 7, 2014 at 19:55
  • Thanks @Tom for the quick reply! I want it to return everything it finds at the end of the string between [ ]. So yeah, in this case i need it to return 0, price, 8. Commented Aug 7, 2014 at 19:58

3 Answers 3

2

You can use a positive lookahead to check if the string ends with the required format:

preg_match_all("/(?=.*(?:\[.*?])+$)\[(.*?)]/", $str, $matches);
$result = $matches[1];

(?:\[.*?])+ matches one or more [xyz] sections. The actual matching is performed only if the lookahead succeeds.

For the string in question, $result would contain:

array(3) {
  [0]=>
  string(1) "0"
  [1]=>
  string(5) "price"
  [2]=>
  string(1) "8"
}
Sign up to request clarification or add additional context in comments.

1 Comment

@DănuțMihaiFlorian: The pattern might look a bit daunting when you're just starting out with lookarounds, but with practice and time, you'll be able to craft regexes easily. For an explanation, see this demo. If you're still confused about something, feel free to ask :)
0

An other way using strrev:

$str = 'field[0][price][8]';

if (preg_match_all('~\G]([^[]*)\[~', strrev($str), $m))
    $result = array_map('strrev', $m[1]);

print_r($result);

Comments

0

If you don't like writing $match[0][$something[0], you can use T-Regx library:

pattern("([\[]([a-zA-Z0-9\_]+)?[\]])+$")
    ->match('field[0][price][8]')
    ->first(function (Match $match) {

       $match->text();   // the matched occurrence

       $match->group(1); // group #1 of the match
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.