i have a string
$str = "this is [start] my string [end] at this moment";
i need to get the content within [start] and [end].
when i use
$result = preg_match('/\[start\].+\[end\]/',$str, $m);
it mutches [start] my string [end], but i need to get only my string(without spaces).
sure i can make another preg_replace and delete them, but i think there must be more elegant solution.
Thanks
Use captured groups.
(Note that your regex will fail if there are multiple
[start]/[end]groups (needs lazy quantifier.+?), or nested[start]/[end]groups (use recursive pattern).)If you don’t want the spaces, you could avoid matching it in the regex:
or just call
trim().