简体   繁体   中英

PHP regex get last characters until match

So I have a string like: "Product name (#15)". Now I want to get the ID out of the string, problem is that the ID of the product can variate in lenght, so I wont be able to just get the last characters. Also the product name may have some special characters.

How can I get the ID with a regex, which should start looking for the number from the right?

pattern = '/.*\(\#(\d+)\)/';
preg_match($pattern, $str, $matches);
print_r($matches);

You can try this -

$str =  "Product name (#15)";

preg_match('/\d+/', $str, $m); // Assuming only the id part will be in digits
echo $m[0]; // 15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM