如何在php preg_match中提取大括号之间的字符串单词和特殊字符

时间:2014-12-18 08:34:46

标签: php regex preg-match preg-match-all

我有像这样的字符串

$string ="this is test {username} ,{password@123} and Other {asdfg@#$}"

我想把这个字符串作为数组格式如下

[1] => Array
        (
            [0] => username
            [1] => password@123
            [2] => asdfg@#$
        )

2 个答案:

答案 0 :(得分:0)

(?<={)[^}]+(?=})

试试这个。这应该有效。

$re = "/(?<={)[^}]+(?=})/mi";
$str = "this is test {username} ,{password@123} and Other {asdfg@#\$}";

preg_match_all($re, $str, $matches);

答案 1 :(得分:0)

$re = "/\{(.*?)\}/";
$str = "this is test {username} ,{password@123} and Other {asdfg@#\$}";

preg_match_all($re, $str, $matches);

print_r($matches);

这给你想要的东西