us_ascii.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * us_ascii encoding functions
  4. *
  5. * takes a string of unicode entities and converts it to a us-ascii encoded string
  6. * Unsupported characters are replaced with ?.
  7. *
  8. * @copyright 2004-2025 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. * @subpackage encode
  13. */
  14. /**
  15. * Converts string to us-ascii
  16. * @param string $string text with numeric unicode entities
  17. * @return string us-ascii encoded text
  18. */
  19. function charset_encode_us_ascii ($string) {
  20. // don't run encoding function, if there is no encoded characters
  21. if (! preg_match("'&#[0-9]+;'",$string) ) return $string;
  22. $string=preg_replace_callback("/&#([0-9]+);/",'unicodetousascii',$string);
  23. return $string;
  24. }
  25. /**
  26. * Return us-ascii symbol when unicode character number is provided
  27. *
  28. * This function is used internally by charset_encode_us_ascii
  29. * function. It might be unavailable to other SquirrelMail functions.
  30. * Don't use it or make sure, that functions/encode/us_ascii.php is
  31. * included.
  32. *
  33. * @param array $matches array with first element a decimal unicode value
  34. * @return string us-ascii character
  35. */
  36. function unicodetousascii($matches) {
  37. $var = $matches[1];
  38. if ($var < 128) {
  39. $ret = chr ($var);
  40. } else {
  41. $ret='?';
  42. }
  43. return $ret;
  44. }