Only digits: Regex and filter_var
<?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"
Written by Andréia Bohner
Related protips
1 Response
$simpler_regex = preg_replace( '/\D/', '', $str);
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Related Tags
#php
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#