2014-01-20 156 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