fork download
  1. <?php
  2.  
  3. $string = 'some gibberish here [tab title="Emergency Contact" important="true"] some additional info here';
  4.  
  5. $regex = '~
  6. (?:\[tab\s # looks for [tab + whitespace literally
  7. | # or
  8. (?!\A)\G\s) # (?!\A) = negative lookahead
  9. # to make sure the following is not the start
  10. # of the string
  11. # \G matches at the end of the previous match
  12. (?P<key>\w+) # match a word character greedily to group "key"
  13. =
  14. "(?P<value>[^"]+)" # " + anything not " + ", save this to group "value"
  15. ~x'; # verbose modifier
  16. preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
  17. foreach ($matches as $match)
  18. echo "Key: {$match['key']}, Value: {$match['value']}\n";
  19. ?>
Success #stdin #stdout 0.04s 52480KB
stdin
Standard input is empty
stdout
Key: title, Value: Emergency Contact
Key: important, Value: true