Mon*_*lue 54 php regex parsing substring
我正在寻找一种使用PHP解析子字符串的方法,并且遇到了preg_match但是我似乎无法解决我需要的规则.
我正在解析一个网页,需要从字符串中获取一个数值,字符串是这样的
producturl.php?id=736375493?=tm
Run Code Online (Sandbox Code Playgroud)
我需要能够获得这部分字符串:
736375493
谢谢亚伦
Dav*_*lls 76
$matches = array();
preg_match('/id=([0-9]+)\?/', $url, $matches);
Run Code Online (Sandbox Code Playgroud)
如果格式改变,这是安全的.如果您在URL中有任何其他号码,slandau的答案将无效.
anu*_*ava 18
<?php
$string = "producturl.php?id=736375493?=tm";
preg_match('~id=(\d+)~', $string, $m );
var_dump($m[1]); // $m[1] is your string
?>
Run Code Online (Sandbox Code Playgroud)
$string = "producturl.php?id=736375493?=tm";
$number = preg_replace("/[^0-9]/", '', $string);
Run Code Online (Sandbox Code Playgroud)