asehgal asehgal - 4 months ago 13
PHP Question

preg match extract digits from string after pattern

I have a string of the form

<something>_string_t_<digits>


How can I extract just the
<digits>
portion in PHP ? I need to detect the string_t first and then pull out the digits.

Answer

You want something like this:

$captures;
preg_match('/string_t_(\d+)/', $string, $captures);
do_something($captures[1]);

$captures[1] will contain <digits> (assuming <digits> is composed entirely of numbers - if it's not, use the more general (.+) in place of (\d+)).

Comments