Search code examples
phpregextagspreg-matchtext-parsing

Match and parse the content of a square-braced placeholder in text


I have a PHP variable ($content) where I need to find a certain pattern that looks like this:

[gallery::name/of/the/folder/]

I would like to search:

- starting with literal characters `[gallery::`
- any other character (variable length)
- ending with "]"

So far, in PHP I have:

preg_match('/\[gallery\:/', $content, $matches, PREG_OFFSET_CAPTURE);

I can find [gallery: but that's it.

I would like to be able to find the rest (:name/of/the/folder/]).


Solution

  • Try capturing it:

    preg_match("/\[gallery::(.*?)]/", $content, $m);
    

    Now $m is an array:

    0 => [gallery::/name/of/the/folder/]
    1 => /name/of/the/folder/