PHP正则表达式帮助:从“page / 2 /”中提取2的快速方法 [英] PHP Regular Expression Help: A quick way to extract the 2 from "page/2/"

查看:267
本文介绍了PHP正则表达式帮助:从“page / 2 /”中提取2的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正则表达式 - 我目前的阿喀琉斯治愈。

Regular expressions - my current Achilles heal.

我只需要一个方法来获取字符串 page / 2 / ,并提取数字,只有号码。它将始终采用该格式。

I just need a way to take the string "page/2/" and extract the number, and only the number. It will always take that format.

所以可以 page / 2 / 页面/ 999999 /

这是我目前拥有的:

preg_match_all('/\/page\/.*\//', 'page/2/', $arr, PREG_PATTERN_ORDER);

如何完成?

推荐答案

通过自选择的分隔符围绕模式,搜索页面/ ,使用特殊匹配组()获取号码 [0-9] ,使用 + 获得至少1位或更多位数字:

Surround the pattern by a self-choosen delimiter #, search for page/, use a special matching group () to get the number [0-9], use the + to get at least 1 or more digits:

$pattern = '#page/([0-9]+)#';
if(preg_match($pattern, $string, $matches)) {
    var_dump($matches[1]);
}

如果你想了解更多,你应该看看 PCRE模式语法手册

If you want to learn more, you should have a look at the PCRE pattern syntax manual on the PHP website.

这篇关于PHP正则表达式帮助:从“page / 2 /”中提取2的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆