Location: PHPKode > scripts > Email Log > wsa_utf8_conversor.php
<?php

/**
 * WSA_UTF8_Conversor Manager
 *
 * @category Final User Manager
 * @package  WSA/UTF8/Conversor
 * @author   Rodrigo Moraes Orph <hide@address.com>
 * @license  Copyright http://www.overflow.biz/
 *
 *  BSD License:
 *
 *  Copyright (c) 2010, Rodrigo Moraes Orph
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this list
 *    of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, this
 *    list of conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *  * Neither the name of the Rodrigo Moraes Orph nor the names of its contributors may be
 *    used to endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 * 
 */

abstract class WSA_UTF8_Conversor {
	protected $_is_utf8_split = null;

	private function __construct() {
		$this->_is_utf8_split = PHP_OS == "WINNT" ? 335 : 5000;
	}

	protected function utf8_encode_array(&$x) { // v1.01
		if (is_array($x)) {
			foreach ($x as &$v) // loop through arrays and/or items
				$v = $this->utf8_encode_array($v);
	
			return $x;
		} else { // not array
			$x = $this->_e2c($x); // -- 100601
	
			return !$this->is_utf8($x) ?
					utf8_encode($x)
				:
					$x
				;
		}
	}
	
	protected function utf8_entity_init() {
		if ($this->_chr == null || $this->_etn == null) {
			$this->_chr = array(
				"á","é","í","ó","ú","ý","Á","É","Í","Ó","Ú","Ý",
				"à","è","ì","ò","ù","À","È","Ì","Ò","Ù",
				"ã","õ","Ã","Õ",
				"ä","ë","ï","ö","ü","ÿ","Ä","Ë","Ï","Ö","Ü",
				"Ç","ç"
			);
			$this->_etn = array(
				"&aacute;","&eacute;","&iacute;","&oacute;","&uacute;","&yacute;","&Aacute;","&Eacute;","&Iacute;","&Oacute;","&Uacute;","&Yacute;",
				"&agrave;","&egrave;","&igrave;","&ograve;","&ugrave;","&Agrave;","&Egrave;","&Igrave;","&Ograve;","&Ugrave;",
				"&atilde;","&otilde;","&Atilde;","&Otilde;",
				"&auml;","&euml;","&iuml;","&ouml;","&uuml;","&yuml;","&Auml;","&Euml;","&Iuml;","&Ouml;","&Uuml;",
				"&Ccedil;","&ccedil;"
			);
			
			for ($i = 161;$i < 256;$i++) { // build chr conversion variables, adding to the previous defined
				$this->_chr[] = chr($i);
				$this->_etn[] = "&#".$i.";";
			}
		}
	}
	
	protected function _e2c($var) { // v1.02
		$this->utf8_entity_init();
	
		if (is_array($var)) {
			foreach ($var as &$x)
				$x = $this->_e2c($x);
	
		} else {
			$var = str_replace($this->_etn,$this->_chr,$var);
		}
	
		return $var;
	}
	
	protected function is_utf8($string) { // v1.03
		if (is_array($string)) {
			foreach ($string as $v) {
				if ($this->is_utf8($string))
					return true;
			}
			return false;
		} elseif (strlen($string) > $this->_is_utf8_split) {
			// Based on: http://mobile-website.mobi/php-utf8-vs-iso-8859-1-59
			for ($s = $i = 0,$j = ceil(strlen($string) / $this->_is_utf8_split);$i < $j;$i++,$s += $this->_is_utf8_split) {
				if (!$this->is_utf8(substr($string,$s,$this->_is_utf8_split))) // this if has been inverted, we are actually tracking the non-utf8 entries
					return false;
			}
			return true; // it's all utf8!
		} else {
	
			// From http://w3.org/International/questions/qa-forms-utf-8.html
			return preg_match('%^(?:
					[\x09\x0A\x0D\x20-\x7E]            # ASCII
				| [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
				|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
				| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
				|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
				|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
				| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
				|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
			)*$%xs', $string);
		}
	}

}
Return current item: Email Log