pheromix
pheromix

Reputation: 19327

How to substract numbers within a string in PHP?

There is a string variable containing number data , say $x = "OP/12/DIR"; . The position of the number data may change at any circumstance by user desire by modifying it inside the application , and the slash bar may be changed by any other character ; but the number data is mandatory. So how to extract the number data from the string ?

Upvotes: 1

Views: 97

Answers (2)

kawashita86
kawashita86

Reputation: 1573

Replace everithing that is not a number:

  $numbers = preg_replace( '/[^\d\.]/', '', $input ); 

or if you will have decimal:

$numbers = preg_replace ( '#\D*?(\d+(\.\d+)?)\D*#', '$1', $input ); 

Upvotes: 1

user657496
user657496

Reputation:

Replace everything that is NOT a number with an empty string.

  $numbers = preg_replace('/[^0-9]*/','',$x);

Upvotes: 4

Related Questions