Advertisement
jessicakennedy1028

Clean Number

Sep 9th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.75 KB | None | 0 0
  1. function clean_number($string, $flags='') {
  2.     $comma = $decimal = $minus = true;
  3.     $parsedFlags = (count(explode(' ', $flags)) > 0 ? explode(' ', $flags) : $flags);
  4.     $allowed = array('NO_COMMA', 'NO_DECIMAL', 'NO_MINUS', 'NUMBERS_ONLY');
  5.     if (is_array ($parsedFlags)) {
  6.         foreach ($parsedFlags as $explodedflag) {
  7.             if (in_array ($explodedflag, $allowed)) {
  8.                 switch (strtoupper ($explodedflag)) {
  9.                     case 'NO_COMMA':
  10.                         $comma = false;
  11.                         break;
  12.                     case 'NO_DECIMAL':
  13.                         $decimal = false;
  14.                         break;
  15.                     case 'NO_MINUS':
  16.                         $minus = false;
  17.                         break;
  18.                     case 'NUMBERS_ONLY':
  19.                         $comma = false;
  20.                         $decimal = false;
  21.                         $minus = false;
  22.                         break;
  23.                 }
  24.             }
  25.         }
  26.     }
  27.     $pattern = "/[^0-9".($comma ? "," : "").($decimal ? "." : "").($minus ? "-" : "")."]+/";
  28.     return preg_replace ($pattern, "", $string);
  29. }
  30.  
  31. # Examples:
  32. $serialnumber = clean_number('SRV293-26731-853-US170711', "NUMBERS_ONLY");
  33. $otherCountries = clean_number('1.542.765,23');
  34. $usCurrency = clean_number('$5,235.95', "NO_COMMA");
  35. $removeMinus = clean_number('-4', "NO_MINUS");
  36. $removeDecimal = clean_number('48.63', "NO_DECIMAL");
  37. $anotherTest = clean_number('-36,326.54', "NO_COMMA NO_MINUS NO_DECIMAL");
  38.  
  39. echo $serialnumber; # output 29326731853170711
  40. echo $otherCountries; # output 1.542.765,23
  41. echo $usCurrency; # output 5235.95
  42. echo $removeMinus; # output 4
  43. echo $removeDecimal; # output 4863
  44. echo $anotherTest; # output 3632654
  45. echo clean_number('4f32k91025'); # output 43291025
  46.  
  47.     $dataset = array(
  48.         "John Adams"=>"SD546-236",
  49.         "William Smith"=>"RQ547-236",
  50.         "Jazz Reynolds"=>"FG542-459",
  51.         "Kimberly Stien"=>"RF321-865",
  52.         "Ian Williams"=>"GG655-962",
  53.         "Frances Teiger"=>"YN653-566",
  54.         "Lynn Merasol"=>"BG345-679",
  55.         "James Deeter"=>"NB654-234",
  56.         "Adrian Monk"=>"CA654-645"
  57.     );
  58.  
  59.     echo "<table style=\"margin: 1em 0; background-color: #f8f9fa; border: 1px solid #a2a9b1; border-collapse: collapse; color: #000;\">";
  60.     echo "<thead style=\"display: table-row-group; vertical-align: middle; border-color: inherit;\">";
  61.     echo "<tr style=\"display: table-row\">";
  62.     echo "<th style=\"background-color: #eaecf0; text-align: center;\">Customer Name</th>";
  63.     echo "<th style=\"background-color: #eaecf0; text-align: center;\">Customer ID</th>";
  64.     echo "</tr>";
  65.     echo "</thead>";
  66.     echo "<tbody>";
  67.     foreach ($dataset as $id=>$data){
  68.         echo "<tr style=\"display: table-row\">";
  69.         echo "<td style=\"border: 1px solid #a2a9b1; padding: 0.2em 0.4em;\">".$id."</td>";
  70.         echo "<td style=\"border: 1px solid #a2a9b1; padding: 0.2em 0.4em;\">".clean_number($data, "NUMBERS_ONLY")."</td>";
  71.         echo "</tr>";
  72.     }
  73.     echo "</tbody>";
  74.     echo "</table>"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement