I’m grabbing a file via file_get_contents($text_file) that has a token in the beginning of the contents in the form…
[widget_my_widget]
The full contents of the file might be…
[widget_my_widget]
Here is the start of the text file. It may have [] brackets inside it,
but only the first occurrence denotes the token.
I’m looking for the regex to grab the text string inside the first occurrence of the brackets []. In this case, I’d want to return widget_my_widget and load it into my variable.
Thanks in advance for your help.
The first captured group in
\[(.+?)\]will match the string inside the square brackets.In PHP you can use it like this:
At first occurance in this string (the file content), ignore the left square bracket, then match as little as possible, but up to (not including) the right square bracket.