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