2013-05-08 69 views
2

此选择括号内的字符串,提取文本不包含在括号

$text = 'ignore everything except this (text)'; 
preg_match('#\((.*?)\)#', $text, $match); 


echo $match[1] . ' /parentheses'; 

我怎么能呼应,是不是括号内的文本?

回答

0

尝试以下操作:

$text = 'ignore everything except this (text)'; 
preg_match('/(.*)\((.*?)\)/', $text, $match); 

echo "in parenthesis: " . $match[2] . "\n"; 
echo "outside parenthesis: " . $match[1] . "\n"; 

不用多说它假定一组括号。

+0

整洁!谢谢!!! – woninana 2013-05-08 05:46:52

0

尝试:

preg_match('^(*.?)\((*.?)\)(*.?)$', $text, $match); 
echo $match[0] . $match[2]; 

假设只有一组括号。