在 使用 Reqular 表达式之间获取字符串


get string in between using Reqular Expression

>我在 php 中有一个字符串作为

$str = "@113_Miscellaneous_0 = 0@,@104_documentFunction_0 = 1@";

如何应用正则表达式,以便我可以提取@字符之间的字符串这样结果的结果将是一个数组,比如

result[0] = "113_Miscellaneous_0 = 0";  
result[1] = "104_Miscellaneous_0 = 1";  

@Fluffeh 感谢您的编辑@乌特卡诺斯 - 尝试过这样的事情

$ptn = "@(.*)@";  
preg_match($ptn, $str, $matches);  
print_r($matches);  
output:
     Array
        (
            [0] => '"113_Miscellaneous_0 = 0'",'"104_documentFunction_0 = 1'"
            [1] => '"113_Miscellaneous_0 = 0'",'"104_documentFunction_0 = 1'"
        )

使用非贪婪匹配,

preg_match_all("/@(.*?)@/", $str, $matches);
var_dump($matches); 

你可以以不同的方式去做:

$str = str_replace("@", "", $str);
$result = explode(",", $str);

编辑

好吧,试一试:

$ptn = "/@(,@)?/";
$str = "@113_Miscellaneous_0 = 0@,@104_documentFunction_0 = 1@";
preg_split($ptn, $str, -1, PREG_SPLIT_NO_EMPTY);

结果:

Array
(
    [0] => 113_Miscellaneous_0 = 0
    [1] => 104_documentFunction_0 = 1
)