Issue #306 - The multibute string library: Corrections after testing.

skala
Ivan Tcholakov 15 years ago
parent 5079fc0e06
commit 1fd25b29ff
  1. 137
      main/inc/lib/multibyte_string_functions.lib.php
  2. 22
      main/inc/lib/multibyte_string_functions_internal.lib.php
  3. 10
      main/inc/lib/upload.xajax.php

@ -285,22 +285,11 @@ function api_htmlentities($string, $quote_style = ENT_COMPAT, $encoding = null)
if (_api_mb_supports($encoding) || _api_iconv_supports($encoding)) {
$string = api_convert_encoding(api_utf8_encode($string, $encoding), 'HTML-ENTITIES', 'UTF-8');
}
elseif (api_is_utf8($encoding)) {
$string = _api_utf8_to_htmlentities($string);
}
elseif (_api_convert_encoding_supports($encoding)) {
if (!api_is_utf8($encoding)) {
$string = _api_convert_encoding($string, 'UTF-8', $encoding);
}
$string = _api_utf8_to_unicode($string);
foreach ($string as $key => &$value) {
if ($value < 128) {
$value = chr($value);
} else {
$value = '&#'.$value.';';
}
}
$string = implode($string);
$string = implode(array_map('_api_html_entity_from_unicode', _api_utf8_to_unicode($string)));
if (!api_is_utf8($encoding)) {
$string = _api_convert_encoding($string, $encoding, 'UTF-8');
}
@ -693,6 +682,17 @@ function api_stripos($haystack, $needle, $offset = 0, $encoding = null) {
if (empty($encoding)){
$encoding = _api_mb_internal_encoding();
}
if (!is_string($needle)) {
$needle = (int)$needle;
if (api_is_utf8($encoding)) {
$needle = _api_utf8_chr($needle);
} else {
$needle = chr($needle);
}
}
if ($needle == '') {
return false;
}
if (_api_mb_supports($encoding)) {
return @mb_stripos($haystack, $needle, $offset, $encoding);
}
@ -736,6 +736,9 @@ function api_stristr($haystack, $needle, $before_needle = false, $encoding = nul
$needle = chr($needle);
}
}
if ($needle == '') {
return false;
}
if (_api_mb_supports($encoding)) {
return @mb_stristr($haystack, $needle, $before_needle, $encoding);
}
@ -818,6 +821,17 @@ function api_strpos($haystack, $needle, $offset = 0, $encoding = null) {
if (empty($encoding)) {
$encoding = _api_mb_internal_encoding();
}
if (!is_string($needle)) {
$needle = (int)$needle;
if (api_is_utf8($encoding)) {
$needle = _api_utf8_chr($needle);
} else {
$needle = chr($needle);
}
}
if ($needle == '') {
return false;
}
if (_api_is_single_byte_encoding($encoding)) {
return strpos($haystack, $needle, $offset);
}
@ -875,6 +889,9 @@ function api_strrchr($haystack, $needle, $before_needle = false, $encoding = nul
$needle = chr($needle);
}
}
if ($needle == '') {
return false;
}
if (_api_is_single_byte_encoding($encoding)) {
if (!$before_needle) {
return strrchr($haystack, $needle);
@ -952,6 +969,17 @@ function api_strrpos($haystack, $needle, $offset = 0, $encoding = null) {
if (empty($encoding)) {
$encoding = _api_mb_internal_encoding();
}
if (!is_string($needle)) {
$needle = (int)$needle;
if (api_is_utf8($encoding)) {
$needle = _api_utf8_chr($needle);
} else {
$needle = chr($needle);
}
}
if ($needle == '') {
return false;
}
if (_api_is_single_byte_encoding($encoding)) {
return strrpos($haystack, $needle, $offset);
}
@ -1026,6 +1054,9 @@ function api_strstr($haystack, $needle, $before_needle = false, $encoding = null
$needle = chr($needle);
}
}
if ($needle == '') {
return false;
}
if (_api_is_single_byte_encoding($encoding)) {
// Adding the missing parameter $before_needle to the original function strstr(), PHP_VERSION < 5.3
if (!$before_needle) {
@ -1239,61 +1270,6 @@ function api_strtoupper($string, $encoding = null) {
return strtoupper($string);
}
/**
* Translates certain characters.
* @param string $string The string being translated.
* @param mixed $from A string that contains the character to be replaced. This parameter can be also an array with pairs of characters 'from' => 'to'.
* @param string $to (optional) A string that contains the replacing characters.
* @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
* @return string Returns a copy of $string, translating all occurrences of each character in $from to the corresponding character in $to.
* This function is aimed at replacing the function strtr() for human-language strings.
* @link http://php.net/manual/en/function.strtr
* TODO: To be revised and tested. Probably this function will not be needed.
* TODO: This function will be removed. It is not needed. 21-AUG-2009.
*/
function api_strtr($string, $from, $to = null, $encoding = null) {
if (empty($string)) {
return '';
}
if (is_array($from)) {
if (empty($from)) {
return $string;
}
$encoding = $to;
if (empty($encoding)){
$encoding = _api_mb_internal_encoding();
}
$translator = $from;
} else {
if (empty($from) || empty($to)) {
return $string;
}
if (empty($encoding)) {
$encoding = _api_mb_internal_encoding();
}
$translator = array();
$arr_from = api_str_split($from, 1, $encoding);
$arr_to = api_str_split($to, 1, $encoding);
$n = count($arr_from);
$n2 = count($arr_to);
if ($n > $n2) $n = $n2;
for ($i = 0; $i < $n; $i++) {
$translator[$arr_from[$i]] = $arr_to[$i];
}
}
$arr_string = api_str_split($string, 1, $encoding);
$n = count($arr_string);
$result = '';
for ($i = 0; $i < $n; $i++) {
if (is_set($translator[$arr_string[$i]])) {
$result .= $translator[$arr_string[$i]];
} else {
$result .= $arr_string[$i];
}
}
return $result;
}
/**
// Gets part of a string.
* @param string $string The input string.
@ -1425,26 +1401,14 @@ function api_substr_replace($string, $replacement, $start, $length = null, $enco
return substr_replace($string, $replacement, $start, $length);
}
if (api_is_encoding_supported($encoding)) {
// This fragment (branch) of code is adaptation of a published proposition:
// http://php.net/manual/en/function.substr-replace.php#90146
$string_length = api_strlen($string, $encoding);
if ($start < 0) {
$start = max(0, $string_length + $start);
}
else if ($start > $string_length) {
$start = $string_length;
}
if ($length < 0) {
$length = max(0, $string_length - $start + $length);
if (is_null($length)) {
$length = api_strlen($string);
}
else if (is_null($length) || ($length > $string_length)) {
$length = $string_length;
if ($start > 0) {
return api_substr($string, 0, $start) . $replacement . api_substr($string, $start + $length);
}
if (($start + $length) > $string_length) {
$length = $string_length - $start;
}
return api_substr($string, 0, $start, $encoding) . $replacement . api_substr($string, $start + $length, $string_length - $start - $length, $encoding);
}
return $replacement . api_substr($string, $start + $length);
}
if (is_null($length)) {
return substr_replace($string, $replacement, $start);
}
@ -2337,6 +2301,9 @@ function api_in_array_nocase($needle, $haystack, $strict = false, $encoding = nu
return in_array($needle, $haystack, $strict);
}
$needle = api_strtolower($needle, $encoding);
if (!is_array($haystack)) {
return false;
}
foreach ($haystack as $item) {
if ($strict && !is_string($item)) {
continue;

@ -341,6 +341,18 @@ function _api_utf8_ord($utf8_character) {
return $codepoints[0];
}
/**
* Makes a html-entity from Unicode codepoint.
* @param int $codepoint The Unicode codepoint.
* @return string Returns the corresponding html-entity; or ASCII character if $codepoint < 128.
*/
function _api_html_entity_from_unicode($codepoint) {
if ($codepoint < 128) {
return chr($codepoint);
}
return '&#'.$codepoint.';';
}
/**
* ----------------------------------------------------------------------------
@ -421,18 +433,12 @@ function _api_utf8_get_letter_case_properties($codepoint, $type = 'lower') {
}
/**
* A callback for serving the function api_ucwords()
* @author Harry Fuecks
* @link http://dev.splitbrain.org/view/darcs/dokuwiki/inc/utf8.php
* @author Ivan Tcholakov, adaptation for the Dokeos LMS, 2009
* A callback for serving the function api_ucwords().
* @param array $matches Input array of matches corresponding to a single word
* @return string Returns a with first char of the word in uppercase
*/
function _api_utf8_ucwords_callback($matches) {
$leadingws = $matches[2];
$ucfirst = api_strtoupper($matches[3], 'UTF-8');
$ucword = api_substr_replace(ltrim($matches[0]), $ucfirst, 0, 1, 'UTF-8');
return $leadingws . $ucword;
return $matches[2] . api_ucfirst(ltrim($matches[0]), 'UTF-8');
}

@ -1,6 +1,6 @@
<?php
include("../global.inc.php");
include(dirname(__FILE__).'/../global.inc.php');
require_once api_get_path(LIBRARY_PATH).'xajax/xajax.inc.php';
$xajax_upload = new Xajax();
$xajax_upload -> registerFunction ('updateProgress');
@ -11,7 +11,7 @@ $xajax_upload -> processRequests();
* @param div_id where the progress bar is displayed
* @param upload_id the identifier given in the field UPLOAD_IDENTIFIER
*/
function updateProgress($div_id, $upload_id, $waitAfterupload=false){
function updateProgress($div_id, $upload_id, $waitAfterupload = false) {
$objResponse = new XajaxResponse();
$ul_info = uploadprogress_get_info($upload_id);
@ -25,13 +25,7 @@ function updateProgress($div_id, $upload_id, $waitAfterupload=false){
$objResponse -> addAssign($div_id.'_label' , 'innerHTML', get_lang('UploadFile').' : '.$percent.' %');
$objResponse -> addAssign($div_id.'_filled' , 'style.width', $percent.'%');
return $objResponse;
}
?>
Loading…
Cancel
Save