GarouDan
GarouDan

Reputation: 3833

PHP. Catching only numbers from a string

If I have a string like

$var = "adf91uaj19jj99877ad009";

Which is the easiest way to get all integers from this variable and create a new one? At this case

$newVar= "911999877009";

Upvotes: 1

Views: 136

Answers (2)

John Kugelman
John Kugelman

Reputation: 361585

Like pounding in a nail with a sledge hammer. Replace all non-digit characters (\D) with nothing ('').

$newVar = preg_replace('/\D/', '', $var);

Upvotes: 5

Baba
Baba

Reputation: 95101

You can try

$var = "adf91uaj19jj99877ad009";
$var = filter_var($var,FILTER_SANITIZE_NUMBER_INT);

Output

string '911999877009' (length=12)

Upvotes: 4

Related Questions