public static function encodeEntities($str, $encoding = 'UTF-8', $convmap = null) { if ($convmap && function_exists('mb_encode_numericentity')) { return mb_encode_numericentity($str, $convmap, $encoding); } return htmlentities($str, ENT_QUOTES, $encoding); }
public static function escape($string, $encoding = null) { if (empty($string)) { return $string; } if (Kint::$enabled_mode === Kint::MODE_CLI) { return str_replace("", '\\x1b', $string); } if (Kint::$enabled_mode === Kint::MODE_WHITESPACE) { return $string; } if ($encoding === null) { $encoding = self::detectEncoding($string); } $string = htmlspecialchars($string, ENT_NOQUOTES, $encoding === 'ASCII' ? 'UTF-8' : $encoding); if ($encoding === 'UTF-8') { // TODO: we could make the symbols hover-title show the code for the invisible symbol // when possible force invisible characters to have some sort of display (experimental) $string = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x80-\\x9F]/u', '?', $string); } // this call converts all non-ASCII characters into html chars of format if (function_exists('mb_encode_numericentity')) { $string = mb_encode_numericentity($string, array(0x80, 0xffff, 0, 0xffff), $encoding); } return $string; }
function courierimage($char, $width = 8, $height = 12, $pdir = 'patterns/') { $im = imagecreate($width, $height); $background_color = imagecolorallocate($im, 255, 255, 255); $text_color = imagecolorallocate($im, 0, 0, 0); $imchar = $char; $imchar = mb_encode_numericentity($imchar, array(0x0, 0xffff, 0, 0xffff), 'UTF-8'); // echo $imchar; imagettftext($im, 10, 0, 0, 10, $text_color, 'cour.ttf', $imchar); $imgarray_0 = img2array($im, $width, $height); $imgarray_1 = img_array_split($imgarray_0); $imgarray_2 = img_array_split($imgarray_1); $ia2name = img_array_name($imgarray_2); if (!file_exists($pdir . $ia2name)) { mkdir($pdir . $ia2name); } $ia1name = img_array_name($imgarray_1); if (!file_exists($pdir . $ia2name . '/' . $ia1name)) { mkdir($pdir . $ia2name . '/' . $ia1name); } $ia0name = img_array_name($imgarray_0); $filename = $pdir . $ia2name . '/' . $ia1name . '/' . $ia0name . '.txt'; if (!file_exists($filename)) { writeUTF8File($filename, $char); } $filename = $pdir . $ia2name . '/' . $ia1name . '/' . $ia0name . '.gif'; if (!file_exists($filename)) { ImageGif($im, $filename); chmod($filename, 0777); } // echo '<table><tr><td>'.print_img_array($imgarray_0).'</td><td>'.print_img_array($imgarray_1).'</td><td>'.print_img_array($imgarray_2)."</td><td><img src=\"$filename\" /></td></table>"; }
/** * Polyfill for json_encode JSON_UNESCAPED_UNICODE (new in PHP 5.4.0) for PHP 5.3 */ function kfJsonEncode($arr) { array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) { $item = mb_encode_numericentity($item, array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); } }); return mb_decode_numericentity(json_encode($arr), array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); }
/** * Convert character encoding from UTF-8 to Shift_JIS. * * @param string $text * @return string */ function _convertUtf8ToSjis($text) { $text = mb_encode_numericentity($text, $this->_utf8map, 'UTF-8'); $text = mb_convert_encoding($text, 'SJIS-win', 'UTF-8'); $pattern = '/&#(6\\d{4});/'; $callback = array($this, '_convertDecimalToSjis'); $text = preg_replace_callback($pattern, $callback, $text); return $text; }
public static function escapeXML($string) { $string = preg_replace('/&/is', '&', $string); $string = preg_replace('/</is', '<', $string); $string = preg_replace('/>/is', '>', $string); $string = preg_replace('/\'/is', ''', $string); $string = preg_replace('/"/is', '"', $string); $string = str_replace(array('ą', 'ć', 'ę', 'ł', 'ń', 'ó', 'ś', 'ź', 'ż', 'Ą', 'Ć', 'Ę', 'Ł', 'Ń', 'Ó', 'Ś', 'Ź', 'Ż', 'ü', 'ò', 'è', 'à', 'ì'), array('a', 'c', 'e', 'l', 'n', 'o', 's', 'z', 'z', 'A', 'C', 'E', 'L', 'N', 'O', 'S', 'Z', 'Z', 'u', 'o', 'e', 'a', 'i'), $string); return mb_encode_numericentity(trim($string), array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); }
function json_encode_readable($arr) { //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) { $item = mb_encode_numericentity($item, array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); } }); return mb_decode_numericentity(json_encode($arr), array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); }
/** * Callback function called by the filter() method. * * This function converts UTF-8 emoji to hexadecimal character reference. * * @param array $matches * @return string */ function _convertUtf8ToEntity($matches) { $utf8 = $matches[0]; $entity = mb_encode_numericentity($utf8, $this->_convmap, 'UTF-8'); if ($utf8 !== $entity) { $unicode = (int) substr($entity, 2, 5); return '&#x' . dechex($unicode) . ';'; } else { return $utf8; } }
public static function make_confusing($string) { $chars = str_split($string); foreach ($chars as &$c) { if (rand(0, 2) != 0) { continue; } $c = mb_encode_numericentity($c, array(0, 0xffff, 0, 0xffff), 'UTF-8'); } return implode('', $chars); }
public static function make_confusing($string) { $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); foreach ($chars as &$c) { if (mt_rand(0, 3) != 0) { $c = utf8tohtml($c); } else { $c = mb_encode_numericentity($c, array(0, 0xffff, 0, 0xffff), 'UTF-8'); } } return implode('', $chars); }
/** * Process contents i.e. insert DropCap at the beginning of the text. * * @param string $content The content to be processed * @param array $options Array of options of how to filter dropcaps * * @return string The processed content */ public function process($content, $options) { // Initialize variables for titling $titling = $options->get('titling.enabled'); $id = md5($content); $breakpoints = preg_quote($options->get('titling.breakpoints'), '~'); $regex = "~.+?(?<=)[{$breakpoints}](?=\\s\\w|\\s*\$)~is"; // Load PHP built-in DOMDocument class if (($dom = $this->loadDOMDocument($content)) === null) { return $content; } // Create a DOM XPath object $xpath = new \DOMXPath($dom); // Get first paragraph of body element $paragraph = $xpath->evaluate('body/p[1]')->item(0); // A paragraph should have at least one node with non-empty content if (!$paragraph || !$paragraph->hasChildNodes()) { return $content; } $textContent = ''; $convmap = array(0x80, 0xffff, 0, 0xffff); foreach ($paragraph->childNodes as $node) { if ($node instanceof \DOMText) { // Make sure that content is UTF-8 and entities properly encoded $text = htmlspecialchars($node->textContent); $text = mb_encode_numericentity($text, $convmap, 'UTF-8'); $textContent .= $text; // Check to match a breakpoint if (preg_match($regex, $textContent, $match)) { $textContent = $match[0]; break; } } else { // Add placeholder to text $textContent .= "{$id}"; } // No breakpoint found... if ($paragraph->lastChild === $node) { return $content; } } // Replace placeholder with regex for matching a XML/HTML tag $re = str_replace("{$id}", '\\s*<\\w+[^>]*>.*?', preg_quote($textContent, '~')); $re = '~(<p[^>]*>)\\s*(' . $re . ')~is'; // Do content replacement $content = preg_replace_callback($re, function ($match) use($paragraph, $options, $debugger) { $content = $this->insertDropCap($match[2]); list($tag, $content) = $this->insertTitling($paragraph, $content, $options->get('titling')); return $tag . $content; }, $content, 1); // Write content back to page return $content; }
function properText($text) { # detect if the string was passed in as unicode $text_encoding = mb_detect_encoding($text, 'UTF-8, ISO-8859-1'); # make sure it's in unicode if ($text_encoding != 'UTF-8') { $text = mb_convert_encoding($text, 'UTF-8', $text_encoding); } # html numerically-escape everything (&#[dec];) $text = mb_encode_numericentity($text, array(0x0, 0xffff, 0, 0xffff), 'UTF-8'); return $text; }
static function utf16Urlencode($str) { # convert characters > 255 into HTML entities $convmap = array(0xff, 0x2ffff, 0, 0xffff); $str = mb_encode_numericentity($str, $convmap, "UTF-8"); # escape HTML entities, so they are not urlencoded $str = preg_replace('/&#([0-9a-fA-F]{2,5});/i', 'mark\\1mark', $str); $str = urlencode($str); # now convert escaped entities into unicode url syntax $str = preg_replace('/mark([0-9a-fA-F]{2,5})mark/i', '%u\\1', $str); return $str; }
public static function encode($value) { @mb_internal_encoding("UTF-8"); if (is_int($value)) { return (string) $value; } elseif (is_string($value)) { $value = str_replace(array('\\', '/', '"', "\r", "\n", "\\b", "\f", "\t"), array('\\\\', '\\/', '\\"', '\\r', '\\n', '\\b', '\\f', '\\t'), $value); $convmap = array(0x80, 0xffff, 0, 0xffff); $result = ""; for ($i = @mb_strlen($value) - 1; $i >= 0; $i--) { $mb_char = @mb_substr($value, $i, 1); if (@mb_ereg("&#(\\d+);", @mb_encode_numericentity($mb_char, $convmap, "UTF-8"), $match)) { $result = sprintf("\\u%04x", $match[1]) . $result; } else { $result = $mb_char . $result; } } return '"' . $result . '"'; } elseif (is_float($value)) { return str_replace(",", ".", $value); } elseif (is_null($value)) { return 'null'; } elseif (is_bool($value)) { return $value ? 'true' : 'false'; } elseif (is_array($value)) { $with_keys = false; $n = count($value); for ($i = 0, reset($value); $i < $n; $i++, next($value)) { if (key($value) !== $i) { $with_keys = true; break; } } } elseif (is_object($value)) { $with_keys = true; } else { return ''; } $result = array(); if ($with_keys) { foreach ($value as $key => $v) { $result[] = self::encode((string) $key) . ':' . self::encode($v); } return '{' . implode(',', $result) . '}'; } else { foreach ($value as $key => $v) { $result[] = self::encode($v); } return '[' . implode(',', $result) . ']'; } }
protected static function _escape($value, $encoding = null) { $encoding or $encoding = self::_detectEncoding($value); if ($encoding === 'UTF-8') { # when possible force invisible characters to have some sort of display $value = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x80-\\x9F]/u', '�', $value); } $value = htmlspecialchars($value, ENT_QUOTES); if (function_exists('mb_encode_numericentity')) { return mb_encode_numericentity($value, array(0x80, 0xffff, 0, 0xffff), $encoding); } else { return $value; } }
/** * Méthode static d'encodage récursif de valeurs dans leurs valeur numériques (é ==> é) * @static * @param mixed $pValue * @return string|array */ public static function toNumericEntities($pValue) { $convmap = array(0x80, 0xff, 0, 0xff); if (is_object($pValue)) { return $pValue; } if (!is_array($pValue)) { return mb_encode_numericentity($pValue, $convmap, Configuration::$global_encoding); } foreach ($pValue as &$value) { $value = self::toNumericEntities($value); } return $pValue; }
/** * @author devilan (REMOVEIT) (at) o2 (dot) pl * For PHP5.3 users who want to emulate JSON_UNESCAPED_UNICODE * @see https://php.net/manual/en/function.json-encode.php#105789 */ public static function associativeArrayToJsonStr($arr, $optionsBitMask = 0) { if (defined('JSON_UNESCAPED_UNICODE')) { return json_encode($arr, JSON_UNESCAPED_UNICODE | $optionsBitMask); } $convmap = array(0x80, 0xffff, 0, 0xffff); //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding array_walk_recursive($arr, function (&$item, $key) use(&$convmap) { if (is_string($item)) { $item = mb_encode_numericentity($item, $convmap, 'UTF-8'); } }); return mb_decode_numericentity(json_encode($arr, $optionsBitMask), $convmap, 'UTF-8'); }
/** * Special handling for multibyte strings (e.g. Unicode) * * @param string $in_str * @return JSON-encoded string */ function json_encode_string($in_str) { mb_internal_encoding("UTF-8"); $convmap = array(0x80, 0xffff, 0, 0xffff); $result = ""; for ($i = mb_strlen($in_str) - 1; $i >= 0; $i--) { $mb_char = mb_substr($in_str, $i, 1); if (mb_ereg("&#(\\d+);", mb_encode_numericentity($mb_char, $convmap, "UTF-8"), $match)) { $result = sprintf("\\u%04x", $match[1]) . $result; } else { $result = $mb_char . $result; } } return $result; }
protected function parseParameters($parameters) { if (!empty($parameters) && is_array($parameters)) { $object = new stdClass(); $parent = $this->getParent(); $object->{$parent} = new stdClass(); $parent = $object->{$parent}; if ($this->actionInclude('/reorder')) { foreach ($parameters as $id) { $item = new stdClass(); $item->id = $id; $parent->{$this->parent}[] = $item; } } else { foreach ($this->fields as $field => $options) { $value = $this->getValue($field, $options, $parameters); if (isset($options['attributes'])) { foreach ($options['attributes'] as $name => $type) { if (null !== $value) { if ($name === 'type') { if ($type === 'array') { if (is_string($value) || is_numeric($value)) { $value = (array) $value; } else { $value = null; } } else { settype($value, $type); } } } } } if (null !== $value) { if (is_string($value)) { $value = mb_encode_numericentity($value, [0x80, 0xffff, 0, 0xffff], 'utf-8'); } !empty($options['sibling']) ? $object->{$field} = $value : ($parent->{$field} = $value); } } } $parameters = json_encode($object); $parameters = mb_decode_numericentity($parameters, [0x80, 0xffff, 0, 0xffff], 'utf-8'); } else { $parameters = '{}'; } return $parameters; }
function superentities($str) { // get rid of existing entities else double-escape $str = html_entity_decode(stripslashes($str), ENT_QUOTES, 'UTF-8'); $ar = preg_split('/(?<!^)(?!$)/u', $str); // return array of every multi-byte character foreach ($ar as $c) { $o = ord($c); if (strlen($c) > 1 || ($o < 32 || $o > 126) || $o > 33 && $o < 40 || $o > 59 && $o < 63) { // convert to numeric entity $c = mb_encode_numericentity($c, array(0x0, 0xffff, 0, 0xffff), 'UTF-8'); } $str2 .= $c; } return $str2; }
/** * Convert all applicable characters to HTML entities * * @param string $text * @return string */ function h($text) { // Convert to utf-8 if (!mb_check_encoding($text, 'UTF-8')) { $text = mb_convert_encoding($text, 'UTF-8'); } // Html encode $text = htmlentities($text, ENT_QUOTES, "UTF-8"); // Convert non printable chars to numeric entity. // \p{Cc} is the Unicode property for control characters, // and the u causes the string to be treated as UTF-8. $text = preg_replace_callback('/\\p{Cc}+/u', function ($match) { return mb_encode_numericentity($match[0], array(0x0, 0xffff, 0, 0xffff), 'UTF-8'); }, $text); return $text; }
function json_encode_string($in_str) { mb_internal_encoding("UTF-8"); $convmap = array(0x80, 0xffff, 0, 0xffff); $mb = function_exists('mb_ereg') ? 'mb_ereg' : 'preg_match'; $regex = function_exists('mb_ereg') ? "&#(\\d+);" : "/&#(\\d+);/u"; $str = ""; for ($i = mb_strlen($in_str) - 1; $i >= 0; $i--) { $mb_char = mb_substr($in_str, $i, 1); if ($mb($regex, mb_encode_numericentity($mb_char, $convmap, "UTF-8"), $match)) { $str = sprintf("\\u%04x", $match[1]) . $str; } else { $str = $mb_char . $str; } } return $str; }
/** * Convert all applicable characters to HTML entities * * @param string $text * @return string */ function gh($text) { // Skip empty strings if ($text === null || $text === '') { return ''; } // Convert to utf-8 if (!mb_check_encoding($text, 'UTF-8')) { $text = mb_convert_encoding($text, 'UTF-8'); } $text = htmlentities($text, ENT_QUOTES, "UTF-8"); // Convert non printable and non ascii chars to numeric entity // This will match a single non-ASCII character // This is a valid PCRE (Perl-Compatible Regular Expression). $text = preg_replace_callback('/[^\\x20-\\x7E]/u', function ($match) { return mb_encode_numericentity($match[0], array(0x0, 0xffff, 0, 0xffff), 'UTF-8'); }, $text); return $text; }
/** * escapeXML * @param string $string * @param string $encoding # character encoding, best to leave it at 'UTF-8' * @return string */ public static function escapeXML($string, $encoding = 'UTF-8') { $string = preg_replace('/&/is', '&', $string); $string = preg_replace('/</is', '<', $string); $string = preg_replace('/>/is', '>', $string); $string = preg_replace('/\'/is', ''', $string); $string = preg_replace('/"/is', '"', $string); $string = str_replace(array('ą', 'ć', 'ę', 'ł', 'ń', 'ó', 'ś', 'ź', 'ż', 'Ą', 'Ć', 'Ę', 'Ł', 'Ń', 'Ó', 'Ś', 'Ź', 'Ż', 'ü'), array('a', 'c', 'e', 'l', 'n', 'o', 's', 'z', 'z', 'A', 'C', 'E', 'L', 'N', 'O', 'S', 'Z', 'Z', 'u'), $string); /** * AAI HACK * * Alternative escaping strategy if the server doesn't * have the php mbstring extension installed/enabled */ $value = htmlspecialchars($string, ENT_QUOTES); if (function_exists('mb_encode_numericentity')) { return mb_encode_numericentity(trim($value), array(0x80, 0xffff, 0, 0xffff), $encoding); } else { return $value; } }
function setSubset($unicode_chars) { $subtable = null; foreach ($this->getData("cmap", "subtables") as $_subtable) { if ($_subtable["platformID"] == 0 || $_subtable["platformID"] == 3 && $_subtable["platformSpecificID"] == 1) { $subtable = $_subtable; break; } } if (!$subtable) { return; } $gids = array(); foreach ($unicode_chars as $char) { $entity = mb_encode_numericentity($char, array(0x0, 0xffff, 0, 0xffff), "utf-8"); $code = (int) preg_replace('`^&#([0-9]+);.*$`', '\\1', $entity); $gids[$code] = $subtable["glyphIndexArray"][$code]; } ksort($gids); $this->glyph_subset = $gids; $this->glyph_all = $subtable["glyphIndexArray"]; }
public function jsonEncode(&$arr) { //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) { $item = mb_encode_numericentity($item, array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); } elseif (is_object($item)) { $reflection = new \ReflectionObject($item); $props = $reflection->getProperties(); $tmp = array(); foreach ($props as $prop) { $name = substr($prop->getName(), 1); $value = ''; try { $method = $reflection->getMethod('get' . ucfirst($name)); $value = $method->invoke($item); } catch (\Exception $ex) { if ($reflection->name == 'org\\autoset\\santorini\\vo\\VirtualFormVO') { $value = $item->__call('get' . ucfirst($name), null); } } if ($value instanceof JSONString) { $value = $value->toString(); } elseif (is_string($value)) { $value = mb_encode_numericentity($value, array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); } elseif (is_array($value)) { $value = json_decode($this->jsonEncode($value)); } elseif (is_object($value)) { $value = $this->jsonEncode($value); } $tmp[$name] = $value; } $item = $tmp; } }); return mb_decode_numericentity(json_encode($arr), array(0x80, 0xffff, 0, 0xffff), 'UTF-8'); }
public function actionVasRegister() { $package_id = Yii::app()->request->getParam('package'); $phone = yii::app()->user->getState('msisdn'); $back_link = Yii::app()->request->getParam('back_link', ''); if ($back_link != '') { Yii::app()->session['back_link'] = $back_link; } if (isset($_GET['link'])) { $requestData = $this->aes->decrypt("{$_GET['link']}"); $composition = explode('&', $requestData); $transactionID = $composition[0]; $msisdnResponse = $composition[1]; $confirm = $composition[2]; $transactionVAS = VasGateModel::model()->findByAttributes(array('transaction_id' => $transactionID)); $package_id = $transactionVAS->package_id; if ($confirm == 1) { if (Formatter::formatPhone($phone) == Formatter::formatPhone($msisdnResponse)) { $this->_register($phone, $package_id, true); $this->redirect(Yii::app()->session['back_link']); } } else { $this->redirect($this->createUrl("/site")); } } else { $check_promotion = $this->check_promotion($phone); $pDetail = PackageModel::model()->findByPk($package_id); $price = $pDetail->fee; $packageCode = $pDetail->code; /*if($check_promotion){ $price = 0; }*/ if ($check_promotion) { $price = 0; if ($package_id == 1) { $fee = ' 2000 đồng/1 ngày'; } else { $fee = ' 7000 đồng/7 ngày'; } $fee .= "|| Khuyến mại 5 ngày"; } else { if ($package_id == 1) { $fee = '1 ngày'; } else { $fee = '7 ngày'; } } $convmap = array(0x80, 0xffff, 0, 0xffff); $fee = mb_encode_numericentity($fee, $convmap, 'UTF-8'); $fee = str_replace("&#", "##", $fee); $vasGate = new VasGateModel(); $vasGate->transaction_id = time() . $phone; $vasGate->package_id = $pDetail->id; $vasGate->information = $pDetail->code; $vasGate->price = $price; if ($vasGate->save()) { $urlGen = new UrlGenerator($this->spId, $vasGate->transaction_id, $packageCode, $price, 'http://amusic.vn/account/vasRegister', $fee); $url = $urlGen->generateUrl($this->aes); $this->redirect($url); } $this->redirect('/'); } }
function encode_high($text, $charset = "UTF-8") { return mb_encode_numericentity($text, $this->cmap(), $charset); }
/** * Converts character codes in the given input from HTML numeric character reference to character code. * * Conversion is done according to Textile's multi-byte conversion map. * * @param string $text The input * @param string $charset The character set * @return string Processed input */ protected function encodeHigh($text, $charset = 'UTF-8') { return $this->mb ? mb_encode_numericentity($text, $this->cmap, $charset) : htmlentities($text, ENT_NOQUOTES, $charset); }
protected static function _escape($value, $encoding = null) { if (Kint::$mode === 'cli' || Kint::$mode === 'whitespace' || empty($value)) { return $value; } $value = htmlspecialchars($value, ENT_QUOTES); if (function_exists('mb_encode_numericentity')) { $encoding or $encoding = self::_detectEncoding($value); $value = mb_encode_numericentity($value, array(0x80, 0xffff, 0, 0xffff), $encoding); } # when possible force invisible characters to have some sort of display (experimental) // todo we could make the symbols hover-title show the code for the invisible symbol return preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x80-\\x9F]/u', '�', $value); }