I need help with getting number from last parentheses in one string. My code take all number from string.
This is my code:
$string = "Mixing group (3pcs) (350) ";
echo (int)preg_replace('/(\D)/', '', $string);
This gives me result : 3350.
But result need to be only number from last parentheses: 350.
My Answer:
You can achieve this by using the following code:
php
$string = "Mixing group (3pcs) (350)";
preg_match('/\((\d+)\)$/', $string, $matches);
echo $matches[1];
This code uses a regular expression to match the last set of parentheses containing numbers at the end of the string. The `\((\d+)\)$` pattern matches a set of parentheses followed by one or more digits at the end of the string. The matched number is stored in the `$matches` array, and you can access it using `$matches[1]`.
Rate this post
5 of 5 based on 3823 votesComments