Only get text within brackets?!?

I’m trying to figure out how to extract only text within brackets ?

This is what I have tried so far:

$str = "I only want this number, [256] to be in my string!";

$out = preg_match("/[(.*?)]/", $str);

echo 'What comes out? '.$out;

I know now that preg_match is NOT the solution… But how then?

Instead of only quick fix, better to read the manual page once to find how the two functions preg_match and [URL=“http://www.php.net/manual/en/function.preg-match-all.php”]preg_match_all are differently work as salathe said.

Ah yes… Thanks alot :wink:


$str = "I only want this number, [256] to be in my string!"; 
preg_match_all("/\\[(.*?)\\]/", $str, $matches); 
echo $matches[1][0];

You’re doing it wrong: preg_match

Have a read of that page (the way you’re using the function isn’t correct for your needs) and also the linked pages outlining the PCRE regular expression syntax (the regex you’ve wrote doesn’t do what you think it should be doing).