0

I have a collection of possible sizes that I would like to trim, see as follows:

quantity_c_size_12_chain

quantity_c_size_12

quantity_size_12_chain

quantity_size_12

quantity_c_size_12_con_c

quantity_c_size_12_con_b

I would like to trim the string of all characters except the integer (in this example, 12)

At the moment I have this:

$size = preg_replace('#\d.*$#', '', $_GET['size']);

Unfortunately this does just the opposite of what I need, it strips the integer from the string and retains the alpha characters i.e. quantity_size_

Can anybody tell me what I need to do to remove everything except the integer?

Thanks,

Aphex22
  • 93
  • 4
  • 11

3 Answers3

1

Try below: Reference

$str = 'quantity_size_12_chain';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
Community
  • 1
  • 1
Rahul Kaushik
  • 1,454
  • 8
  • 16
1
$size = preg_replace("/\D+/", '', $_GET['size']);

where \D are non-digits, as opposed to \d

mpapec
  • 50,217
  • 8
  • 67
  • 127
0
$str = 'quantity_c_size_12_chain';
$new_str = preg_replace('/[^0-9]/','',$str);
dikesh
  • 2,977
  • 2
  • 16
  • 26