Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 "<table style=\"margin: 1em 0; background-color: #f8f9fa; border: 1px solid #a2a9b1; border-collapse: collapse; color: #000;\">";
- echo "<thead style=\"display: table-row-group; vertical-align: middle; border-color: inherit;\">";
- echo "<tr style=\"display: table-row\">";
- echo "<th style=\"background-color: #eaecf0; text-align: center;\">Customer Name</th>";
- echo "<th style=\"background-color: #eaecf0; text-align: center;\">Customer ID</th>";
- echo "</tr>";
- echo "</thead>";
- echo "<tbody>";
- foreach ($dataset as $id=>$data){
- echo "<tr style=\"display: table-row\">";
- echo "<td style=\"border: 1px solid #a2a9b1; padding: 0.2em 0.4em;\">".$id."</td>";
- echo "<td style=\"border: 1px solid #a2a9b1; padding: 0.2em 0.4em;\">".clean_number($data, "NUMBERS_ONLY")."</td>";
- echo "</tr>";
- }
- echo "</tbody>";
- echo "</table>"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement