2012-02-10 91 views
1

如何选择两个括号之间的字符串的一部分。选择两个括号之间的字符串的一部分

一个例子选择kanych(kanych) Kurva Ggglo

+1

可能重复的方式(http://stackoverflow.com/questions/7636319/selecting-a-part-of -a-string) – JJJ 2012-02-10 09:03:08

+0

你能否确认实际的字符串和实际输出的期望值?因为有人编辑了你的问题,所以我不确定实际的起始字符串! – ManseUK 2012-02-10 09:04:56

回答

4

您可以使用正则表达式:

/ .../ begin and end 
\(... \) the brackets of your choice (round brackets have to be escaped) 
(...)  remember the content 
[^\)]*  the content, every character except the bracket 

PHP:

$sTest = "(kanych) Kurva Ggglo"; 
preg_match("/\(([^\)]*)\)/", $sTest, $aMatches); 
$sResult = $aMatches[1]; 

另见this example

P.s .:与preg_match_all(...)您可以从字符串中获取所有括号内容。

+0

它的工作原理。 Tnx =) – 2012-02-10 09:17:49

+0

不客气。如果你能接受你的问题的正确答案,这将是公平的。 – scessor 2012-02-10 09:27:31

2
$str = "lolo (kanya) momomo"; 
$openstrpos = strpos($str,"("); 
$closestrpos = strpos($str,")"); 
$finalstr = substr($str, $openstrpos+1, $closestrpos-$openstrpos-1); 

一个你可以得到所需的输出[选择字符串的一部分]的

相关问题