Get content from bracket
With preg_match how can I get the string between the bracket
E开发者_如何学Goxample: sdsdds (sdsd) sdsdsd
And I want the
sdsd
preg_match('/\(([^\)]*)\)/', 'sdsdds (sdsd) sdsdsd', $matches);
echo $matches[1]; // sdsd
Matches characters within parentheses, including blank values. If you want to match multiple instances, you can use preg_match_all.
preg_match('/\((.*?)\)/', $text, $a);
echo $a[1];
The simplest:
#\(([^\)]+)\)#
It's not very readable, because all the (
and )
must be escaped with \
.
The #
are delimiters.
Using preg_match
:
$str = 'sdsdds (sdsd) sdsdsd';
$iMatches = preg_match('#\(([^\)]+)\)#', $str, $aMatches);
echo $aMatches[1]; // 'sdsd'
精彩评论