Trying To Find Forward Slash In Preg_Match

落爺英雄遲暮 提交于 2019-12-05 14:24:35

/ is not the only thing you can use as a delimiter. In fact, you can use almost any non-slphanumeric character. Personally I like to use () because it reminds me that the first item of the result array is the entire match and it also never needs escaping in the pattern.

preg_match("(^/([a-z]+)/(\d+)$)i",$samplerequesturi,$out);
var_dump($out);

That should do it.

Your problem may be that you are using the / forward-slash as a regex delimiter (at the start and end of the regex expression). Switch to using a character other than the forward-slash, such as a # hash symbol or any other symbol which will never need to appear in this particular expression. Then you won't need to escape the forward-slash character at all in the expression.

If you want to use regex (which I don't think is necessary in this case, simply splitting on "/" should be fine:

$samplerequesturi = "/variable/12345678910";
preg_match("@^/([A-Za-z]+)/(\d+)$@", $samplerequesturi, $out);
echo $out[1];
echo $out[2];

should get you going

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!