Skip to content
Advertisement

extract numbers from a string if they meet a specific condition [closed]

In the following string there are some numbers followed by two letters “ST”. How can I extract all of those numbers?

$str = '104 ST Lorem 104 ipsum (dolor) sit ST, 2000 ST adipiscing 2 2 ST elit.'

In the above string there is 104 ST, 2000 ST and 2 ST.

Advertisement

Answer

We can use preg_match_all here with the pattern (d+) STb:

$str = "104 ST Lorem 104 ipsum (dolor) sit ST, 2000 ST adipiscing 2 2 ST elit.";
preg_match_all("/(d+) STb/", $str, $matches);
print_r($matches[1]);

This prints:

Array
(
    [0] => 104
    [1] => 2000
    [2] => 2
)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement