PHP - Need to fill array from text
I need to fill an array from all text, within a string that begins with [
and ends 开发者_StackOverflow社区with ]
.
For Example:
here is some text [to do] and something else [/find] for the way this works is [object] and [tada]
This should return an array like so:
array('[to do]', '[/find]', '[object]', '[tada]');
How can I do this quickly? And in the same order that's in within the text?
There can be any characters at all within the square brackets. I just need to grab them all, plus both brackets.
$subject = "here is some text [to do] and something else [/find] for the way this works is [object] and [tada]";
preg_match_all("/\[[^\[\]]+\]/", $subject, $out);
var_dump($out[0]);
You can use preg_match_all
.
preg_match_all('/\[(.*?)\]/', $text_to_filter, $array);
$array
will contain your matches.
It won't work with nested square brackets, though.
精彩评论