/openconstructor/lib/third/phputf8/utils/ascii.php
PHP | 220 lines | 109 code | 13 blank | 98 comment | 11 complexity | fbb30cb009dd68e9c25973af928770d1 MD5 | raw file
Possible License(s): GPL-2.0
- <?php
- /**
- * Tools to help with ASCII in UTF-8
- * @version $Id: ascii.php,v 1.2 2007/03/15 06:56:30 sanjar Exp $
- * @package utf8
- * @subpackage ascii
- */
-
- //--------------------------------------------------------------------
- /**
- * Tests whether a string contains only 7bit ASCII bytes.
- * You might use this to conditionally check whether a string
- * needs handling as UTF-8 or not, potentially offering performance
- * benefits by using the native PHP equivalent if it's just ASCII e.g.;
- *
- * <code>
- * if ( utf8_is_ascii($someString) ) {
- * // It's just ASCII - use the native PHP version
- * $someString = strtolower($someString);
- * } else {
- * $someString = utf8_strtolower($someString);
- * }
- * </code>
- *
- * @param string
- * @return boolean TRUE if it's all ASCII
- * @package utf8
- * @subpackage ascii
- * @see utf8_is_ascii_ctrl
- */
- function utf8_is_ascii($str) {
- // Search for any bytes which are outside the ASCII range...
- return (preg_match('/(?:[^\x00-\x7F])/',$str) !== 1);
- }
-
- //--------------------------------------------------------------------
- /**
- * Tests whether a string contains only 7bit ASCII bytes with device
- * control codes omitted. The device control codes can be found on the
- * second table here: http://www.w3schools.com/tags/ref_ascii.asp
- *
- * @param string
- * @return boolean TRUE if it's all ASCII without device control codes
- * @package utf8
- * @subpackage ascii
- * @see utf8_is_ascii
- */
- function utf8_is_ascii_ctrl($str) {
- if ( strlen($str) > 0 ) {
- // Search for any bytes which are outside the ASCII range,
- // or are device control codes
- return (preg_match('/[^\x09\x0A\x0D\x20-\x7E]/',$str) !== 1);
- }
- return FALSE;
- }
-
- //--------------------------------------------------------------------
- /**
- * Strip out all non-7bit ASCII bytes
- * If you need to transmit a string to system which you know can only
- * support 7bit ASCII, you could use this function.
- * @param string
- * @return string with non ASCII bytes removed
- * @package utf8
- * @subpackage ascii
- * @see utf8_strip_non_ascii_ctrl
- */
- function utf8_strip_non_ascii($str) {
- ob_start();
- while ( preg_match(
- '/^([\x00-\x7F]+)|([^\x00-\x7F]+)/S',
- $str, $matches) ) {
- if ( !isset($matches[2]) ) {
- echo $matches[0];
- }
- $str = substr($str, strlen($matches[0]));
- }
- $result = ob_get_contents();
- ob_end_clean();
- return $result;
- }
-
- //--------------------------------------------------------------------
- /**
- * Strip out device control codes in the ASCII range
- * which are not permitted in XML. Note that this leaves
- * multi-byte characters untouched - it only removes device
- * control codes
- * @see http://hsivonen.iki.fi/producing-xml/#controlchar
- * @param string
- * @return string control codes removed
- */
- function utf8_strip_ascii_ctrl($str) {
- ob_start();
- while ( preg_match(
- '/^([^\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)|([\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+)/S',
- $str, $matches) ) {
- if ( !isset($matches[2]) ) {
- echo $matches[0];
- }
- $str = substr($str, strlen($matches[0]));
- }
- $result = ob_get_contents();
- ob_end_clean();
- return $result;
- }
-
- //--------------------------------------------------------------------
- /**
- * Strip out all non 7bit ASCII bytes and ASCII device control codes.
- * For a list of ASCII device control codes see the 2nd table here:
- * http://www.w3schools.com/tags/ref_ascii.asp
- *
- * @param string
- * @return boolean TRUE if it's all ASCII
- * @package utf8
- * @subpackage ascii
- */
- function utf8_strip_non_ascii_ctrl($str) {
- ob_start();
- while ( preg_match(
- '/^([\x09\x0A\x0D\x20-\x7E]+)|([^\x09\x0A\x0D\x20-\x7E]+)/S',
- $str, $matches) ) {
- if ( !isset($matches[2]) ) {
- echo $matches[0];
- }
- $str = substr($str, strlen($matches[0]));
- }
- $result = ob_get_contents();
- ob_end_clean();
- return $result;
- }
-
- //---------------------------------------------------------------
- /**
- * Replace accented UTF-8 characters by unaccented ASCII-7 "equivalents".
- * The purpose of this function is to replace characters commonly found in Latin
- * alphabets with something more or less equivalent from the ASCII range. This can
- * be useful for converting a UTF-8 to something ready for a filename, for example.
- * Following the use of this function, you would probably also pass the string
- * through utf8_strip_non_ascii to clean out any other non-ASCII chars
- * Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1)
- * letters. Default is to deaccent both cases ($case = 0)
- *
- * For a more complete implementation of transliteration, see the utf8_to_ascii package
- * available from the phputf8 project downloads:
- * http://prdownloads.sourceforge.net/phputf8
- *
- * @param string UTF-8 string
- * @param int (optional) -1 lowercase only, +1 uppercase only, 1 both cases
- * @param string UTF-8 with accented characters replaced by ASCII chars
- * @return string accented chars replaced with ascii equivalents
- * @author Andreas Gohr <andi@splitbrain.org>
- * @package utf8
- * @subpackage ascii
- */
- function utf8_accents_to_ascii( $str, $case=0 ){
-
- static $UTF8_LOWER_ACCENTS = NULL;
- static $UTF8_UPPER_ACCENTS = NULL;
-
- if($case <= 0){
-
- if ( is_null($UTF8_LOWER_ACCENTS) ) {
- $UTF8_LOWER_ACCENTS = array(
- 'ŕ' => 'a', 'ô' => 'o', '?' => 'd', '?' => 'f', 'ë' => 'e', '' => 's', '?' => 'o',
- 'ß' => 'ss', '?' => 'a', '?' => 'r', '?' => 't', '?' => 'n', '?' => 'a', '?' => 'k',
- '?' => 's', '?' => 'y', '?' => 'n', '?' => 'l', '?' => 'h', '?' => 'p', 'ó' => 'o',
- 'ú' => 'u', '?' => 'e', 'é' => 'e', 'ç' => 'c', '?' => 'w', '?' => 'c', 'ő' => 'o',
- '?' => 's', 'ř' => 'o', '?' => 'g', '?' => 't', '?' => 's', '?' => 'e', '?' => 'c',
- '?' => 's', 'î' => 'i', '?' => 'u', '?' => 'c', '?' => 'e', '?' => 'w', '?' => 't',
- '?' => 'u', '?' => 'c', 'ö' => 'oe', 'č' => 'e', '?' => 'y', '?' => 'a', '?' => 'l',
- '?' => 'u', '?' => 'u', '?' => 's', '?' => 'g', '?' => 'l', '' => 'f', '' => 'z',
- '?' => 'w', '?' => 'b', 'ĺ' => 'a', 'ě' => 'i', 'ď' => 'i', '?' => 'd', '?' => 't',
- '?' => 'r', 'ä' => 'ae', 'í' => 'i', '?' => 'r', 'ę' => 'e', 'ü' => 'ue', 'ň' => 'o',
- '?' => 'e', 'ń' => 'n', '?' => 'n', '?' => 'h', '?' => 'g', '?' => 'd', '?' => 'j',
- '˙' => 'y', '?' => 'u', '?' => 'u', '?' => 'u', '?' => 't', 'ý' => 'y', '?' => 'o',
- 'â' => 'a', '?' => 'l', '?' => 'w', '?' => 'z', '?' => 'i', 'ă' => 'a', '?' => 'g',
- '?' => 'm', '?' => 'o', '?' => 'i', 'ů' => 'u', '?' => 'i', '?' => 'z', 'á' => 'a',
- 'ű' => 'u', 'ţ' => 'th', 'đ' => 'dh', 'ć' => 'ae', 'ľ' => 'u', '?' => 'e',
- );
- }
-
- $str = str_replace(
- array_keys($UTF8_LOWER_ACCENTS),
- array_values($UTF8_LOWER_ACCENTS),
- $str
- );
- }
-
- if($case >= 0){
- if ( is_null($UTF8_UPPER_ACCENTS) ) {
- $UTF8_UPPER_ACCENTS = array(
- 'Ŕ' => 'A', 'Ô' => 'O', '?' => 'D', '?' => 'F', 'Ë' => 'E', '' => 'S', '?' => 'O',
- '?' => 'A', '?' => 'R', '?' => 'T', '?' => 'N', '?' => 'A', '?' => 'K',
- '?' => 'S', '?' => 'Y', '?' => 'N', '?' => 'L', '?' => 'H', '?' => 'P', 'Ó' => 'O',
- 'Ú' => 'U', '?' => 'E', 'É' => 'E', 'Ç' => 'C', '?' => 'W', '?' => 'C', 'Ő' => 'O',
- '?' => 'S', 'Ř' => 'O', '?' => 'G', '?' => 'T', '?' => 'S', '?' => 'E', '?' => 'C',
- '?' => 'S', 'Î' => 'I', '?' => 'U', '?' => 'C', '?' => 'E', '?' => 'W', '?' => 'T',
- '?' => 'U', '?' => 'C', 'Ö' => 'Oe', 'Č' => 'E', '?' => 'Y', '?' => 'A', '?' => 'L',
- '?' => 'U', '?' => 'U', '?' => 'S', '?' => 'G', '?' => 'L', '?' => 'F', '' => 'Z',
- '?' => 'W', '?' => 'B', 'Ĺ' => 'A', 'Ě' => 'I', 'Ď' => 'I', '?' => 'D', '?' => 'T',
- '?' => 'R', 'Ä' => 'Ae', 'Í' => 'I', '?' => 'R', 'Ę' => 'E', 'Ü' => 'Ue', 'Ň' => 'O',
- '?' => 'E', 'Ń' => 'N', '?' => 'N', '?' => 'H', '?' => 'G', '?' => 'D', '?' => 'J',
- '' => 'Y', '?' => 'U', '?' => 'U', '?' => 'U', '?' => 'T', 'Ý' => 'Y', '?' => 'O',
- 'Â' => 'A', '?' => 'L', '?' => 'W', '?' => 'Z', '?' => 'I', 'Ă' => 'A', '?' => 'G',
- '?' => 'M', '?' => 'O', '?' => 'I', 'Ů' => 'U', '?' => 'I', '?' => 'Z', 'Á' => 'A',
- 'Ű' => 'U', 'Ţ' => 'Th', 'Đ' => 'Dh', 'Ć' => 'Ae', '?' => 'E',
- );
- }
- $str = str_replace(
- array_keys($UTF8_UPPER_ACCENTS),
- array_values($UTF8_UPPER_ACCENTS),
- $str
- );
- }
-
- return $str;
-
- }