e870b9ff9036292dc061e884ecb83a5ce02d92eb
[squirrelmail.git] / functions / encode / us_ascii.php
1 <?php
2
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 *
9 * @copyright 2004-2012 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
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 */
21 function 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
25 $string=preg_replace("/&#([0-9]+);/e","unicodetousascii('\\1')",$string);
26
27 return $string;
28 }
29
30 /**
31 * Return us-ascii symbol when unicode character number is provided
32 *
33 * This function is used internally by charset_encode_us_ascii
34 * function. It might be unavailable to other SquirrelMail functions.
35 * Don't use it or make sure, that functions/encode/us_ascii.php is
36 * included.
37 *
38 * @param int $var decimal unicode value
39 * @return string us-ascii character
40 */
41 function unicodetousascii($var) {
42
43 if ($var < 128) {
44 $ret = chr ($var);
45 } else {
46 $ret='?';
47 }
48 return $ret;
49 }