function clean_number($string, $flags='') { $comma = $decimal = $minus = true; $parsedFlags = (count(explode(' ', $flags)) > 0 ? explode(' ', $flags) : $flags); $allowed = array('NO_COMMA', 'NO_DECIMAL', 'NO_MINUS', 'NUMBERS_ONLY'); if (is_array ($parsedFlags)) { foreach ($parsedFlags as $explodedflag) { if (in_array ($explodedflag, $allowed)) { switch (strtoupper ($explodedflag)) { case 'NO_COMMA': $comma = false; break; case 'NO_DECIMAL': $decimal = false; break; case 'NO_MINUS': $minus = false; break; case 'NUMBERS_ONLY': $comma = false; $decimal = false; $minus = false; break; } } } } $pattern = "/[^0-9".($comma ? "," : "").($decimal ? "." : "").($minus ? "-" : "")."]+/"; return preg_replace ($pattern, "", $string); } # Examples: $serialnumber = clean_number('SRV293-26731-853-US170711', "NUMBERS_ONLY"); $otherCountries = clean_number('1.542.765,23'); $usCurrency = clean_number('$5,235.95', "NO_COMMA"); $removeMinus = clean_number('-4', "NO_MINUS"); $removeDecimal = clean_number('48.63', "NO_DECIMAL"); $anotherTest = clean_number('-36,326.54', "NO_COMMA NO_MINUS NO_DECIMAL"); echo $serialnumber; # output 29326731853170711 echo $otherCountries; # output 1.542.765,23 echo $usCurrency; # output 5235.95 echo $removeMinus; # output 4 echo $removeDecimal; # output 4863 echo $anotherTest; # output 3632654 echo clean_number('4f32k91025'); # output 43291025 $dataset = array( "John Adams"=>"SD546-236", "William Smith"=>"RQ547-236", "Jazz Reynolds"=>"FG542-459", "Kimberly Stien"=>"RF321-865", "Ian Williams"=>"GG655-962", "Frances Teiger"=>"YN653-566", "Lynn Merasol"=>"BG345-679", "James Deeter"=>"NB654-234", "Adrian Monk"=>"CA654-645" ); echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; foreach ($dataset as $id=>$data){ echo ""; echo ""; echo ""; echo ""; } echo ""; echo "
Customer NameCustomer ID
".$id."".clean_number($data, "NUMBERS_ONLY")."
"