diff options
author | Christian Schneider | 2012-03-28 13:00:39 +0000 |
---|---|---|
committer | Christian Schneider | 2012-03-28 13:00:39 +0000 |
commit | b1c0b4946572027c8de564730a89ec584c830bf3 (patch) | |
tree | 81859c280f43a5cdb31ddede7300641fa530e0f8 /it.class | |
parent | 49d2a5ce1b6ad201f051263db7c3a1f5ad6a39ab (diff) | |
download | itools-b1c0b4946572027c8de564730a89ec584c830bf3.tar.gz itools-b1c0b4946572027c8de564730a89ec584c830bf3.tar.bz2 itools-b1c0b4946572027c8de564730a89ec584c830bf3.zip |
Added it::any2utf8, fixed it::replace fast path to add u modified, added error reporting for invalid utf-8 input to it::match and it::replace
Diffstat (limited to 'it.class')
-rw-r--r-- | it.class | 18 |
1 files changed, 17 insertions, 1 deletions
@@ -356,6 +356,8 @@ static function match($pattern, $string, $p = null) { if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) it::error("Exceeded pcre.backtrack_limit of " . ini_get('pcre.backtrack_limit') . " bytes"); + else if (preg_last_error() == PREG_BAD_UTF8_ERROR) + it::error("Input to it::match is not valid utf-8"); $result = $p['all'] ? array() : null; } @@ -379,11 +381,15 @@ static function match($pattern, $string, $p = null) */ static function replace($replacements, $string, $p = array()) { + $encoding = ini_get('default_charset') == 'utf-8' ? 'u' : ''; foreach ($replacements as $pattern => $dummy) - $patterns[] = !preg_match('/\\\\[wb]|[!\x80-\xff]|\[\[:/i', $pattern) && !$p ? "!$pattern!i" : it::convertregex($pattern, $p); + $patterns[] = !preg_match('/\\\\[wb]|[!\x80-\xff]|\[\[:/i', $pattern) && !$p ? "!$pattern!i$encoding" : it::convertregex($pattern, $p); $result = preg_replace($patterns, $replacements, $string, isset($p['limit']) ? $p['limit'] : -1); + if ($result === null && preg_last_error() == PREG_BAD_UTF8_ERROR) + it::error("Input to it::replace is not valid utf-8"); + return $result; } @@ -404,6 +410,16 @@ static function grep($pattern, $array, $p = array()) } /** + * Convert string to utf8 if it was not already utf-8 before + * @param $value String to convert + * @return Same string in utf-8 encoding + */ +function any2utf8($value) +{ + return strlen($value) && strlen(htmlspecialchars($value, 0, 'utf-8')) == 0 ? utf8_encode($value) : $value; # Use side-effect of htmlspecialchars: Fails if not valid utf-8 encoding +} + +/** * Uppercase first character similar to ucfirst() but for mbstring.internal_encoding */ static function ucfirst($string) |