很抱歉删除了另一个 preg_match
正则表达式问题。我已经处理这个问题太久了,无法让它工作,所以感谢任何帮助!!
我需要一个简单的 preg_match_all 来返回格式为 [@varname]
的占位符。
在字符串中
"hello, this is [@firstname], i am [@age] years old"
我希望得到一个包含“firstname”和“age”值的数组。
我对[varname]
的尝试是有效的:
preg_match_all("/[([^]]+)]/", $t, $result);
我试图包含@character的任何事情都失败了......
preg_match_all("/[\@([^]]+)]/", $t, $result);
最佳答案
代码:
$str = 'hello, this is [@firstname], i am [@age] years old';
preg_match_all('~\[@(.+?)\]~', $str, $matches);
print_r($matches);
输出:
Array
(
[0] => Array
(
[0] => [@firstname]
[1] => [@age]
)
[1] => Array
(
[0] => firstname
[1] => age
)
)
关于PHP preg_match 占位符 [@varname],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18725810/