Skip to content

Instantly share code, notes, and snippets.

@ArupSen
Created November 14, 2012 15:59
Show Gist options
  • Save ArupSen/4072951 to your computer and use it in GitHub Desktop.
Save ArupSen/4072951 to your computer and use it in GitHub Desktop.
extract numbers from string regexp
// To remove anything that is not a number:
$output = preg_replace('/[^0-9]/', '', $input);
/*
Explanation:
[0-9] matches any number between 0 and 9 inclusively.
^ negates a [] pattern.
So, [^0-9] matches anything that is not a number, and since we're using preg_replace, they will be replaced by nothing '' (second argument of preg_replace).
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment