【发布时间】:2017-07-14 23:30:29
【问题描述】:
我有字符串
$string = 'foo/{id}/bar/{name}';
我正在尝试进行正则表达式过滤
想要的输出:
$matches = [
0 => 'foo/{id}/bar/{name}',
1 => 'id',
2 => 'name'
]
到目前为止:(正则表达式是我的弱点)
preg_match('~^' . $magic . '$~', $string, $matches)
编辑:url中有*n个{variable}
【问题讨论】:
-
你为什么不用
explode()呢? -
刀就够了,为什么还要用电锯?使用
explode() -
{id}和{name}是占位符还是文字?也许3v4l.org/eo0kO? (取决于“名称”的定义) -
@chris85
{id}和{name}是文字 -
不是答案,但这个regex101.com 对于调试正则表达式非常有用
标签: php regex preg-match