ascii_strip.phps

  1. <?php
  2.  
  3. // Strip non-printable characters
  4. function strip_ascii($string, $pad = '') {
  5. $ret = "";
  6. $len = strlen($string);
  7. for($a=0; $a<$len; $a++) {
  8. $p = ord($string[$a]);
  9. (($p > 31 && $p < 127)) ? $ret .= $string[$a] : $ret .= $pad;
  10. }
  11. return $ret;
  12. }
  13.  
  14. // Strip all but the specified allowed characters from string
  15. function strip($data, $allowed) {
  16. if (strlen($data) == 0 || strlen($allowed) == 0) {
  17. return false;
  18. }
  19. foreach (str_split($allowed) as $char) {
  20. $arrAllow[ord($char)] = $char;
  21. }
  22. $result = '';
  23. foreach (str_split($data) as $char) {
  24. if (isset($arrAllow[ord($char)])) {
  25. $result .= $char;
  26. }
  27. }
  28. return $result;
  29. }
  30.  
  31.  
  32.  
  33. ?>