Preg_replace 在字符串中查找数字

Preg_replace find numbers in string

下午好,

我的 preg_replace 有错误。我的字符串是 Cat-115/Tid-11 我想在 Cat-115/Tid- 之后找到任何数字并打印

你能帮我解决这个问题吗?我敢肯定,这是一些基本错误,对此深感抱歉...

我的代码:

$destination = $_GET['q']; Out ////Cat-115/Tid-11
$numbers = preg_replace('/[^0-9]/', '', $destination);
if (strpos($destination, $numbers) !== false) {

}

谢谢!

假设 Cat-115/Tid- 是一个固定的字符串,而你想要 "find any numbers after Cat-115/Tid- and print that",你可以使用 preg_match:

使用\K重置报告比赛的起点:

$destination = "Cat-115/Tid-11";
preg_match('/Cat-115\/Tid-\K\d+/', $destination, $m);
echo $m[0];

或者捕获数字是一个捕获组(\d+)

preg_match('/Cat-115\/Tid-(\d+)/', $destination, $m);
echo $m[1];

Output