Skip to content

Instantly share code, notes, and snippets.

@andreia
Created April 16, 2012 18:23
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andreia/2400504 to your computer and use it in GitHub Desktop.
Only digits
<?php
$str = '13++56-461.79/27--14-abc';
$filter = filter_var($str,FILTER_SANITIZE_NUMBER_INT);
$onlyDigitsFilterVar = str_replace(array('+', '-'), '', $filter);
$onlyDigitsRegexp1 = preg_replace('/[^\d]/', '', $str);
$onlyDigitsRegexp2 = preg_replace('/[^[:digit:]]/', '', $str); // http://www.php.net/manual/en/regexp.reference.character-classes.php
var_dump($filter); // string(19) "13++56-4617927--14-"
var_dump($onlyDigitsFilterVar); // string(13) "1356461792714"
var_dump($onlyDigitsRegexp1); // string(13) "1356461792714"
var_dump($onlyDigitsRegexp2); // string(13) "1356461792714"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment