0

I need to fill an array from all text, within a string that begins with [ and ends 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.

1
  • In the PHP Language. Sorry bout that. Commented Sep 19, 2011 at 7:35

2 Answers 2

1
$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]);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.