Reputation: 3833
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
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
Reputation: 95101
You can try
$var = "adf91uaj19jj99877ad009";
$var = filter_var($var,FILTER_SANITIZE_NUMBER_INT);
Output
string '911999877009' (length=12)
Upvotes: 4