0

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

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

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

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

@Fluffeh 感谢您编辑 @Utkanos - 尝试过这样的事情

$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\"
        )
4

2 回答 2

3

使用非贪婪匹配,

preg_match_all("/@(.*?)@/", $str, $matches);
var_dump($matches); 
于 2012-08-13T11:20:01.813 回答
1

你可能会采取不同的方式:

$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
)
于 2012-08-13T11:22:38.347 回答