PHP使用RegEx获取字符串的子字符串 [英] PHP Using RegEx to get substring of a string

查看:76
本文介绍了PHP使用RegEx获取字符串的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用PHP解析子字符串的方法,并且遇到了preg_match,但是我似乎无法解决我需要的规则.

I'm looking for an way to parse a substring using PHP, and have come across preg_match however I can't seem to work out the rule that I need.

我正在解析网页,需要从字符串中获取一个数值,字符串就像这样

I am parsing a web page and need to grab a numeric value from the string, the string is like this

producturl.php?id=736375493?=tm

我需要能够获得字符串的这一部分:

I need to be able to obtain this part of the string:

736375493

736375493

感谢亚伦

推荐答案

$matches = array();
preg_match('/id=([0-9]+)\?/', $url, $matches);

如果格式更改,这是安全的.如果您的URL中有其他数字,则slandau的答案将无效.

This is safe for if the format changes. slandau's answer won't work if you ever have any other numbers in the URL.

php.net/preg-match

这篇关于PHP使用RegEx获取字符串的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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