2014-01-20 157 views
1

我有字符串,例如:带分隔符PHP单独字符串

$stringExample = "(({FAPAGE15}+500)/{GOGA:V18})" 
// separete content { } 

我需要的结果是类似的东西:

$response = array("FAPAGE15","GOGA:V18") 

我认为它必须与东西:preg_splitpreg_match

回答

1

这里是你需要的正则表达式:

\{(.*?)\} 

正则表达式例如:

http://regex101.com/r/qU8eB0

PHP:

$str = "(({FAPAGE15}+500)/{GOGA:V18})"; 

preg_match_all("/\{(.*?)\}/", $str, $matches); 

print_r($matches[1]); 

输出:

Array 
(
    [0] => FAPAGE15 
    [1] => GOGA:V18 
) 

工作实施例:

https://eval.in/92516

+0

谢谢soo !! :d – user3216962

1

可以使用负字符类:[^}](所有这不是一个}

preg_match_all('~(?<={)[^}]++(?=})~', $str, $matches); 

$result = $matches[0]; 

图案的详细资料

~   # pattern delimiter 
(?<={) # preceded by { 
[^}]++ # all that is not a } one or more times (possessive) 
(?=})  # followed by } 
~   # pattern delimiter 

注:占有欲量词++是不是必须有好的结果可以用+来代替。您可以找到有关此功能的更多信息here