Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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