[英]PHP Using RegEx to get substring of a string
我正在尋找一種使用PHP解析子字符串的方法,並且遇到了preg_match但是我似乎無法解決我需要的規則。
我正在解析一個網頁,需要從字符串中獲取一個數值,字符串是這樣的
producturl.php?id=736375493?=tm
我需要能夠獲得這部分字符串:
736375493
謝謝亞倫
$matches = array();
preg_match('/id=([0-9]+)\?/', $url, $matches);
如果格式改變,這是安全的。 如果您在URL中有任何其他號碼,slandau的答案將無效。
<?php
$string = "producturl.php?id=736375493?=tm";
preg_match('~id=(\d+)~', $string, $m );
var_dump($m[1]); // $m[1] is your string
?>
$string = "producturl.php?id=736375493?=tm";
$number = preg_replace("/[^0-9]/", '', $string);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.