You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2234 lines
84 KiB
2234 lines
84 KiB
![]()
16 years ago
|
<?php
|
||
![]()
16 years ago
|
/* For licensing terms, see /license.txt */
|
||
|
|
||
![]()
8 years ago
|
use Chamilo\CoreBundle\Framework\Container;
|
||
![]()
6 years ago
|
use ChamiloSession as Session;
|
||
![]()
9 years ago
|
use Patchwork\Utf8;
|
||
![]()
8 years ago
|
use Westsworld\TimeAgo;
|
||
![]()
9 years ago
|
|
||
![]()
16 years ago
|
/**
|
||
|
* File: internationalization.lib.php
|
||
![]()
9 years ago
|
* Internationalization library for Chamilo 1.x LMS
|
||
![]()
16 years ago
|
* A library implementing internationalization related functions.
|
||
![]()
8 years ago
|
* License: GNU General Public License Version 3 (Free Software Foundation)ww.
|
||
|
*
|
||
![]()
16 years ago
|
* @author Ivan Tcholakov, <ivantcholakov@gmail.com>, 2009, 2010
|
||
![]()
16 years ago
|
* @author More authors, mentioned in the correpsonding fragments of this source.
|
||
![]()
8 years ago
|
*
|
||
![]()
16 years ago
|
* @package chamilo.library
|
||
![]()
16 years ago
|
*/
|
||
![]()
16 years ago
|
// Predefined date formats in Chamilo provided by the language sub-system.
|
||
![]()
14 years ago
|
// To be used as a parameter for the function api_format_date()
|
||
![]()
12 years ago
|
define('TIME_NO_SEC_FORMAT', 0); // 15:23
|
||
|
define('DATE_FORMAT_SHORT', 1); // Aug 25, 09
|
||
|
define('DATE_FORMAT_LONG', 2); // Monday August 25, 09
|
||
|
define('DATE_FORMAT_LONG_NO_DAY', 10); // August 25, 2009
|
||
|
define('DATE_TIME_FORMAT_LONG', 3); // Monday August 25, 2009 at 03:28 PM
|
||
![]()
14 years ago
|
|
||
![]()
12 years ago
|
define('DATE_FORMAT_NUMBER', 4); // 25.08.09
|
||
![]()
13 years ago
|
define('DATE_TIME_FORMAT_LONG_24H', 5); // August 25, 2009 at 15:28
|
||
![]()
12 years ago
|
define('DATE_TIME_FORMAT_SHORT', 6); // Aug 25, 2009 at 03:28 PM
|
||
|
define('DATE_TIME_FORMAT_SHORT_TIME_FIRST', 7); // 03:28 PM, Aug 25 2009
|
||
|
define('DATE_FORMAT_NUMBER_NO_YEAR', 8); // 25.08 dd-mm
|
||
|
define('DATE_FORMAT_ONLY_DAYNAME', 9); // Monday, Sunday, etc
|
||
![]()
14 years ago
|
|
||
![]()
16 years ago
|
// Formatting person's name.
|
||
![]()
12 years ago
|
// Formatting a person's name using the pattern as it has been
|
||
|
// configured in the internationalization database for every language.
|
||
|
// This (default) option would be the most used.
|
||
|
define('PERSON_NAME_COMMON_CONVENTION', 0);
|
||
|
// The following options may be used in limited number of places for overriding the common convention:
|
||
|
|
||
|
// Formatting a person's name in Western order: first_name last_name
|
||
|
define('PERSON_NAME_WESTERN_ORDER', 1);
|
||
|
// Formatting a person's name in Eastern order: last_name first_name
|
||
|
define('PERSON_NAME_EASTERN_ORDER', 2);
|
||
|
// Contextual: formatting person's name in library order: last_name, first_name
|
||
|
define('PERSON_NAME_LIBRARY_ORDER', 3);
|
||
![]()
10 years ago
|
// Contextual: formatting a person's name assotiated with an email-address.
|
||
|
// Ivan: I am not sure how seems email servers an clients would interpret name order, so I assign the Western order.
|
||
![]()
12 years ago
|
define('PERSON_NAME_EMAIL_ADDRESS', PERSON_NAME_WESTERN_ORDER);
|
||
![]()
10 years ago
|
// Contextual: formatting a person's name for data-exporting operations.
|
||
|
// For backward compatibility this format has been set to Eastern order.
|
||
![]()
12 years ago
|
define('PERSON_NAME_DATA_EXPORT', PERSON_NAME_EASTERN_ORDER);
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
8 years ago
|
* Returns a translated (localized) string.
|
||
|
*
|
||
![]()
8 years ago
|
* @param string $variable
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @return string
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function get_lang($variable)
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
$translator = Container::getTranslator();
|
||
|
|
||
|
if (!$translator) {
|
||
|
return $variable;
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
// Using symfony
|
||
![]()
8 years ago
|
$defaultDomain = 'messages';
|
||
![]()
6 years ago
|
$locale = api_get_language_isocode();
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
$translated = $translator->trans(
|
||
![]()
8 years ago
|
$variable,
|
||
![]()
8 years ago
|
[],
|
||
![]()
6 years ago
|
$defaultDomain,
|
||
|
$locale
|
||
![]()
8 years ago
|
);
|
||
|
|
||
![]()
6 years ago
|
if ($translated === $variable) {
|
||
![]()
8 years ago
|
// Check the langVariable for BC
|
||
![]()
6 years ago
|
$translated = $translator->trans(
|
||
![]()
8 years ago
|
"lang$variable",
|
||
![]()
8 years ago
|
[],
|
||
![]()
6 years ago
|
$defaultDomain,
|
||
|
$locale
|
||
![]()
8 years ago
|
);
|
||
|
|
||
![]()
6 years ago
|
if ($translated === "lang$variable") {
|
||
![]()
8 years ago
|
return $variable;
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
return $translated;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
|
* Gets the current interface language.
|
||
![]()
8 years ago
|
*
|
||
|
* @param bool $purified (optional) When it is true, a purified (refined)
|
||
|
* language value will be returned, for example 'french' instead of 'french_unicode'
|
||
![]()
9 years ago
|
* @param bool $setParentLanguageName
|
||
![]()
8 years ago
|
*
|
||
|
* @return string the current language of the interface
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_get_interface_language(
|
||
|
$purified = false,
|
||
|
$check_sub_language = false,
|
||
|
$setParentLanguageName = true
|
||
|
) {
|
||
![]()
8 years ago
|
return api_get_language_isocode();
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a purified language id, without possible suffixes that will disturb language identification in certain cases.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string the same purified or filtered language id, for example 'french'
|
||
|
*
|
||
![]()
9 years ago
|
* @return string
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_purify_language_id($language)
|
||
|
{
|
||
![]()
8 years ago
|
static $purified = [];
|
||
![]()
15 years ago
|
if (!isset($purified[$language])) {
|
||
![]()
9 years ago
|
$purified[$language] = trim(
|
||
|
str_replace(
|
||
![]()
8 years ago
|
['_unicode', '_latin', '_corporate', '_org', '_km'],
|
||
![]()
9 years ago
|
'',
|
||
|
strtolower($language)
|
||
|
)
|
||
|
);
|
||
![]()
15 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $purified[$language];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
6 years ago
|
* Gets language iso code.
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_get_language_isocode()
|
||
![]()
10 years ago
|
{
|
||
![]()
6 years ago
|
$request = Container::getRequest();
|
||
|
if ($request) {
|
||
|
return $request->getLocale();
|
||
|
}
|
||
|
|
||
|
return 'en';
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
15 years ago
|
/**
|
||
![]()
8 years ago
|
* Gets language iso code column from the language table.
|
||
![]()
13 years ago
|
*
|
||
![]()
8 years ago
|
* @return array An array with the current isocodes
|
||
![]()
13 years ago
|
*
|
||
![]()
15 years ago
|
* */
|
||
![]()
10 years ago
|
function api_get_platform_isocodes()
|
||
|
{
|
||
![]()
6 years ago
|
$list = [];
|
||
![]()
9 years ago
|
$sql = "SELECT isocode
|
||
|
FROM ".Database::get_main_table(TABLE_MAIN_LANGUAGE)."
|
||
![]()
9 years ago
|
ORDER BY isocode ";
|
||
![]()
6 years ago
|
$result = Database::query($sql);
|
||
|
if (Database::num_rows($result)) {
|
||
|
while ($row = Database::fetch_array($result)) {
|
||
|
$list[] = trim($row['isocode']);
|
||
![]()
15 years ago
|
}
|
||
![]()
13 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
6 years ago
|
return $list;
|
||
![]()
15 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
|
* Gets text direction according to the given language.
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @param string $language This is the name of the
|
||
![]()
8 years ago
|
* folder containing translations for the corresponding language (e.g 'arabic', 'english', ...).
|
||
|
* ISO-codes are acceptable too ('ar', 'en', ...).
|
||
|
* If $language is omitted, interface language is assumed then.
|
||
|
*
|
||
|
* @return string the correspondent to the language text direction ('ltr' or 'rtl')
|
||
![]()
16 years ago
|
*/
|
||
![]()
10 years ago
|
function api_get_text_direction($language = null)
|
||
|
{
|
||
![]()
8 years ago
|
static $text_direction = [];
|
||
![]()
11 years ago
|
|
||
![]()
14 years ago
|
if (empty($language)) {
|
||
![]()
9 years ago
|
$language = api_get_interface_language();
|
||
![]()
13 years ago
|
}
|
||
![]()
15 years ago
|
if (!isset($text_direction[$language])) {
|
||
![]()
9 years ago
|
$text_direction[$language] = in_array(
|
||
|
api_purify_language_id($language),
|
||
![]()
8 years ago
|
[
|
||
![]()
13 years ago
|
'arabic',
|
||
![]()
14 years ago
|
'ar',
|
||
![]()
13 years ago
|
'dari',
|
||
![]()
14 years ago
|
'prs',
|
||
![]()
13 years ago
|
'hebrew',
|
||
![]()
14 years ago
|
'he',
|
||
![]()
15 years ago
|
'iw',
|
||
![]()
13 years ago
|
'pashto',
|
||
![]()
14 years ago
|
'ps',
|
||
![]()
13 years ago
|
'persian',
|
||
![]()
14 years ago
|
'fa',
|
||
![]()
15 years ago
|
'ur',
|
||
![]()
13 years ago
|
'yiddish',
|
||
![]()
8 years ago
|
'yid',
|
||
![]()
8 years ago
|
]
|
||
![]()
15 years ago
|
) ? 'rtl' : 'ltr';
|
||
|
}
|
||
![]()
10 years ago
|
|
||
![]()
15 years ago
|
return $text_direction[$language];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
9 years ago
|
* Returns an alphabetized list of timezones in an associative array
|
||
![]()
8 years ago
|
* that can be used to populate a select.
|
||
![]()
16 years ago
|
*
|
||
|
* @return array List of timezone identifiers
|
||
|
*
|
||
|
* @author Guillaume Viguier <guillaume.viguier@beeznest.com>
|
||
|
*/
|
||
![]()
12 years ago
|
function api_get_timezones()
|
||
|
{
|
||
![]()
15 years ago
|
$timezone_identifiers = DateTimeZone::listIdentifiers();
|
||
|
sort($timezone_identifiers);
|
||
![]()
8 years ago
|
$out = [];
|
||
![]()
15 years ago
|
foreach ($timezone_identifiers as $tz) {
|
||
|
$out[$tz] = $tz;
|
||
|
}
|
||
![]()
8 years ago
|
$null_option = ['' => ''];
|
||
![]()
15 years ago
|
$result = array_merge($null_option, $out);
|
||
![]()
9 years ago
|
|
||
![]()
15 years ago
|
return $result;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Returns the timezone to be converted to/from, based on user or admin preferences.
|
||
|
*
|
||
![]()
16 years ago
|
* @return string The timezone chosen
|
||
|
*/
|
||
![]()
9 years ago
|
function api_get_timezone()
|
||
![]()
11 years ago
|
{
|
||
![]()
6 years ago
|
$timezone = Session::read('system_timezone');
|
||
|
if (empty($timezone)) {
|
||
|
// First, get the default timezone of the server
|
||
|
$timezone = date_default_timezone_get();
|
||
|
// Second, see if a timezone has been chosen for the platform
|
||
|
$timezoneFromSettings = api_get_setting('timezone_value', 'timezones');
|
||
|
|
||
|
if ($timezoneFromSettings != null) {
|
||
|
$timezone = $timezoneFromSettings;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
6 years ago
|
// If allowed by the administrator
|
||
|
$allowUserTimezones = api_get_setting('use_users_timezone', 'timezones');
|
||
![]()
11 years ago
|
|
||
![]()
6 years ago
|
if ($allowUserTimezones === 'true') {
|
||
![]()
8 years ago
|
$userId = api_get_user_id();
|
||
|
// Get the timezone based on user preference, if it exists
|
||
![]()
6 years ago
|
$newExtraField = new ExtraFieldValue('user');
|
||
|
$data = $newExtraField->get_values_by_handler_and_field_variable($userId, 'timezone');
|
||
|
if (!empty($data) && isset($data['timezone']) && !empty($data['timezone'])) {
|
||
|
$timezone = $data['timezone'];
|
||
![]()
8 years ago
|
}
|
||
![]()
15 years ago
|
}
|
||
![]()
6 years ago
|
Session::write('system_timezone', $timezone);
|
||
![]()
15 years ago
|
}
|
||
![]()
11 years ago
|
|
||
![]()
6 years ago
|
return $timezone;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
10 years ago
|
* Returns the given date as a DATETIME in UTC timezone.
|
||
|
* This function should be used before entering any date in the DB.
|
||
![]()
16 years ago
|
*
|
||
![]()
6 years ago
|
* @param mixed $time Date to be converted (can be a string supported by date() or a timestamp)
|
||
|
* @param bool $returnNullIfInvalidDate If the date is not correct return null instead of the current date
|
||
|
* @param bool $returnObj Returns a DateTime object
|
||
![]()
10 years ago
|
*
|
||
![]()
7 years ago
|
* @return string|DateTime The DATETIME in UTC to be inserted in the DB,
|
||
|
* or null if the format of the argument is not supported
|
||
![]()
16 years ago
|
*
|
||
![]()
13 years ago
|
* @author Julio Montoya - Adding the 2nd parameter
|
||
![]()
16 years ago
|
* @author Guillaume Viguier <guillaume.viguier@beeznest.com>
|
||
|
*/
|
||
![]()
8 years ago
|
function api_get_utc_datetime(
|
||
|
$time = null,
|
||
![]()
8 years ago
|
$returnNullIfInvalidDate = false,
|
||
![]()
8 years ago
|
$returnObj = false
|
||
|
) {
|
||
![]()
9 years ago
|
if (is_null($time) || empty($time) || $time === '0000-00-00 00:00:00') {
|
||
![]()
8 years ago
|
if ($returnNullIfInvalidDate) {
|
||
![]()
13 years ago
|
return null;
|
||
|
}
|
||
![]()
10 years ago
|
if ($returnObj) {
|
||
![]()
7 years ago
|
return new DateTime(gmdate('Y-m-d H:i:s'), new DateTimeZone('UTC'));
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
15 years ago
|
return gmdate('Y-m-d H:i:s');
|
||
|
}
|
||
![]()
11 years ago
|
|
||
![]()
15 years ago
|
// If time is a timestamp, return directly in utc
|
||
![]()
15 years ago
|
if (is_numeric($time)) {
|
||
![]()
8 years ago
|
$time = (int) $time;
|
||
![]()
10 years ago
|
|
||
![]()
15 years ago
|
return gmdate('Y-m-d H:i:s', $time);
|
||
|
}
|
||
|
try {
|
||
![]()
8 years ago
|
$fromTimezone = api_get_timezone();
|
||
|
$date = new DateTime($time, new DateTimezone($fromTimezone));
|
||
![]()
8 years ago
|
$date->setTimezone(new DateTimeZone('UTC'));
|
||
![]()
10 years ago
|
if ($returnObj) {
|
||
|
return $date;
|
||
|
} else {
|
||
|
return $date->format('Y-m-d H:i:s');
|
||
|
}
|
||
![]()
15 years ago
|
} catch (Exception $e) {
|
||
|
return null;
|
||
|
}
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Returns a DATETIME string converted to the right timezone.
|
||
|
*
|
||
![]()
6 years ago
|
* @param mixed $time The time to be converted
|
||
|
* @param string $to_timezone The timezone to be converted to.
|
||
|
* If null, the timezone will be determined based on user preference,
|
||
|
* or timezone chosen by the admin for the platform.
|
||
|
* @param string $from_timezone The timezone to be converted from. If null, UTC will be assumed.
|
||
|
* @param bool $returnNullIfInvalidDate
|
||
|
* @param bool $showTime
|
||
|
* @param bool $humanForm
|
||
|
* @param string $format
|
||
![]()
8 years ago
|
*
|
||
![]()
16 years ago
|
* @return string The converted time formatted as Y-m-d H:i:s
|
||
|
*
|
||
|
* @author Guillaume Viguier <guillaume.viguier@beeznest.com>
|
||
|
*/
|
||
![]()
9 years ago
|
function api_get_local_time(
|
||
![]()
9 years ago
|
$time = null,
|
||
|
$to_timezone = null,
|
||
|
$from_timezone = null,
|
||
![]()
6 years ago
|
$returnNullIfInvalidDate = false,
|
||
![]()
9 years ago
|
$showTime = true,
|
||
![]()
6 years ago
|
$humanForm = false,
|
||
|
$format = ''
|
||
![]()
9 years ago
|
) {
|
||
|
// Determining the timezone to be converted from
|
||
|
if (is_null($from_timezone)) {
|
||
|
$from_timezone = 'UTC';
|
||
|
}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
// If time is a timestamp, convert it to a string
|
||
|
if (is_null($time) || empty($time) || $time == '0000-00-00 00:00:00') {
|
||
![]()
6 years ago
|
if ($returnNullIfInvalidDate) {
|
||
![]()
9 years ago
|
return null;
|
||
|
}
|
||
|
$from_timezone = 'UTC';
|
||
|
$time = gmdate('Y-m-d H:i:s');
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
if (is_numeric($time)) {
|
||
![]()
7 years ago
|
$time = (int) $time;
|
||
![]()
6 years ago
|
if ($returnNullIfInvalidDate) {
|
||
![]()
8 years ago
|
if (strtotime(date('d-m-Y H:i:s', $time)) !== (int) $time) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
$from_timezone = 'UTC';
|
||
|
$time = gmdate('Y-m-d H:i:s', $time);
|
||
|
}
|
||
![]()
6 years ago
|
|
||
![]()
9 years ago
|
if ($time instanceof DateTime) {
|
||
|
$time = $time->format('Y-m-d H:i:s');
|
||
|
$from_timezone = 'UTC';
|
||
|
}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
try {
|
||
![]()
8 years ago
|
// Determining the timezone to be converted to
|
||
|
if (is_null($to_timezone)) {
|
||
|
$to_timezone = api_get_timezone();
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
$date = new DateTime($time, new DateTimezone($from_timezone));
|
||
|
$date->setTimezone(new DateTimeZone($to_timezone));
|
||
![]()
9 years ago
|
|
||
![]()
6 years ago
|
if (!empty($format)) {
|
||
|
return $date->format($format);
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
return api_get_human_date_time($date, $showTime, $humanForm);
|
||
![]()
9 years ago
|
} catch (Exception $e) {
|
||
![]()
9 years ago
|
return '';
|
||
![]()
9 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
8 years ago
|
* Converts a string into a timestamp safely (handling timezones), using strtotime.
|
||
![]()
16 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $time to be converted
|
||
![]()
9 years ago
|
* @param string $timezone (if null, the timezone will be determined based
|
||
![]()
8 years ago
|
* on user preference, or timezone chosen by the admin for the platform)
|
||
|
*
|
||
![]()
16 years ago
|
* @return int Timestamp
|
||
![]()
16 years ago
|
*
|
||
![]()
16 years ago
|
* @author Guillaume Viguier <guillaume.viguier@beeznest.com>
|
||
|
*/
|
||
![]()
9 years ago
|
function api_strtotime($time, $timezone = null)
|
||
|
{
|
||
![]()
15 years ago
|
$system_timezone = date_default_timezone_get();
|
||
![]()
13 years ago
|
if (!empty($timezone)) {
|
||
|
date_default_timezone_set($timezone);
|
||
|
}
|
||
![]()
15 years ago
|
$timestamp = strtotime($time);
|
||
![]()
8 years ago
|
if (!empty($timezone)) {
|
||
|
// only reset timezone if it was changed
|
||
|
date_default_timezone_set($system_timezone);
|
||
|
}
|
||
![]()
9 years ago
|
|
||
![]()
15 years ago
|
return $timestamp;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
10 years ago
|
* Returns formatted date/time, correspondent to a given language.
|
||
|
* The given date should be in the timezone chosen by the administrator
|
||
|
* and/or user. Use api_get_local_time to get it.
|
||
![]()
16 years ago
|
*
|
||
![]()
16 years ago
|
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
|
||
|
* @author Christophe Gesche<gesche@ipm.ucl.ac.be>
|
||
|
* originally inspired from from PhpMyAdmin
|
||
|
* @author Ivan Tcholakov, 2009, code refactoring, adding support for predefined date/time formats.
|
||
![]()
16 years ago
|
* @author Guillaume Viguier <guillaume.viguier@beeznest.com>
|
||
![]()
16 years ago
|
*
|
||
![]()
8 years ago
|
* @param mixed $time Timestamp or datetime string
|
||
![]()
8 years ago
|
* @param string|int Date format (see date formats in the Chamilo system:
|
||
|
* TIME_NO_SEC_FORMAT,
|
||
|
* DATE_FORMAT_SHORT,
|
||
|
* DATE_FORMAT_LONG,
|
||
|
* DATE_TIME_FORMAT_LONG
|
||
|
* @param string $language (optional) Language id
|
||
![]()
8 years ago
|
* If it is omitted, the current interface language is assumed
|
||
|
*
|
||
|
* @return string returns the formatted date
|
||
![]()
16 years ago
|
*
|
||
![]()
8 years ago
|
* @see http://php.net/manual/en/function.strftime.php
|
||
![]()
16 years ago
|
*/
|
||
![]()
10 years ago
|
function api_format_date($time, $format = null, $language = null)
|
||
|
{
|
||
![]()
8 years ago
|
if (empty($time)) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
![]()
11 years ago
|
$system_timezone = date_default_timezone_get();
|
||
![]()
9 years ago
|
date_default_timezone_set(api_get_timezone());
|
||
![]()
11 years ago
|
|
||
![]()
15 years ago
|
if (is_string($time)) {
|
||
|
$time = strtotime($time);
|
||
|
}
|
||
|
|
||
|
if (is_null($format)) {
|
||
|
$format = DATE_TIME_FORMAT_LONG;
|
||
|
}
|
||
|
|
||
|
$datetype = null;
|
||
|
$timetype = null;
|
||
|
|
||
![]()
14 years ago
|
if (is_int($format)) {
|
||
![]()
15 years ago
|
switch ($format) {
|
||
![]()
13 years ago
|
case DATE_FORMAT_ONLY_DAYNAME:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateFormatOnlyDayName', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::SHORT;
|
||
|
$timetype = IntlDateFormatter::NONE;
|
||
|
|
||
![]()
13 years ago
|
break;
|
||
![]()
13 years ago
|
case DATE_FORMAT_NUMBER_NO_YEAR:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateFormatShortNumberNoYear', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::SHORT;
|
||
|
$timetype = IntlDateFormatter::NONE;
|
||
|
|
||
![]()
9 years ago
|
break;
|
||
|
case DATE_FORMAT_NUMBER:
|
||
|
$date_format = get_lang('dateFormatShortNumber', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::SHORT;
|
||
|
$timetype = IntlDateFormatter::NONE;
|
||
|
|
||
![]()
13 years ago
|
break;
|
||
![]()
15 years ago
|
case TIME_NO_SEC_FORMAT:
|
||
![]()
11 years ago
|
$date_format = get_lang('timeNoSecFormat', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::NONE;
|
||
|
$timetype = IntlDateFormatter::SHORT;
|
||
|
|
||
![]()
15 years ago
|
break;
|
||
|
case DATE_FORMAT_SHORT:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateFormatShort', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::LONG;
|
||
|
$timetype = IntlDateFormatter::NONE;
|
||
|
|
||
![]()
15 years ago
|
break;
|
||
|
case DATE_FORMAT_LONG:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateFormatLong', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::FULL;
|
||
|
$timetype = IntlDateFormatter::NONE;
|
||
|
|
||
![]()
15 years ago
|
break;
|
||
|
case DATE_TIME_FORMAT_LONG:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateTimeFormatLong', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::FULL;
|
||
|
$timetype = IntlDateFormatter::SHORT;
|
||
|
|
||
![]()
13 years ago
|
break;
|
||
![]()
13 years ago
|
case DATE_FORMAT_LONG_NO_DAY:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateFormatLongNoDay', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::FULL;
|
||
|
$timetype = IntlDateFormatter::SHORT;
|
||
|
|
||
![]()
13 years ago
|
break;
|
||
![]()
9 years ago
|
case DATE_TIME_FORMAT_SHORT:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateTimeFormatShort', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::FULL;
|
||
|
$timetype = IntlDateFormatter::SHORT;
|
||
|
|
||
![]()
14 years ago
|
break;
|
||
![]()
9 years ago
|
case DATE_TIME_FORMAT_SHORT_TIME_FIRST:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateTimeFormatShortTimeFirst', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::FULL;
|
||
|
$timetype = IntlDateFormatter::SHORT;
|
||
|
|
||
![]()
14 years ago
|
break;
|
||
|
case DATE_TIME_FORMAT_LONG_24H:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateTimeFormatLong24H', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::FULL;
|
||
|
$timetype = IntlDateFormatter::SHORT;
|
||
|
|
||
![]()
15 years ago
|
break;
|
||
|
default:
|
||
![]()
11 years ago
|
$date_format = get_lang('dateTimeFormatLong', '', $language);
|
||
![]()
6 years ago
|
|
||
|
$datetype = IntlDateFormatter::FULL;
|
||
|
$timetype = IntlDateFormatter::SHORT;
|
||
![]()
15 years ago
|
}
|
||
|
}
|
||
![]()
13 years ago
|
|
||
![]()
6 years ago
|
// Use ICU
|
||
|
if (is_null($language)) {
|
||
|
$language = api_get_language_isocode();
|
||
![]()
11 years ago
|
}
|
||
![]()
6 years ago
|
$date_formatter = new IntlDateFormatter($language, $datetype, $timetype, date_default_timezone_get());
|
||
|
//$date_formatter->setPattern($date_format);
|
||
|
$formatted_date = api_to_system_encoding($date_formatter->format($time), 'UTF-8');
|
||
|
|
||
![]()
11 years ago
|
date_default_timezone_set($system_timezone);
|
||
![]()
9 years ago
|
|
||
![]()
15 years ago
|
return $formatted_date;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
9 years ago
|
* Returns the difference between the current date (date(now)) with the parameter
|
||
|
* $date in a string format like "2 days ago, 1 hour ago".
|
||
![]()
9 years ago
|
* You can use it like this:
|
||
![]()
8 years ago
|
* Display::dateToStringAgoAndLongDate($dateInUtc);.
|
||
![]()
9 years ago
|
*
|
||
![]()
7 years ago
|
* @param string $date Result of a date function in this format -> date('Y-m-d H:i:s', time());
|
||
![]()
9 years ago
|
* @param string $timeZone
|
||
![]()
7 years ago
|
* @param bool $returnDateDifference
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @return string
|
||
![]()
16 years ago
|
*
|
||
|
* @author Julio Montoya
|
||
|
*/
|
||
![]()
7 years ago
|
function date_to_str_ago($date, $timeZone = 'UTC', $returnDateDifference = false)
|
||
![]()
10 years ago
|
{
|
||
![]()
9 years ago
|
if ($date === '0000-00-00 00:00:00') {
|
||
![]()
10 years ago
|
return '';
|
||
|
}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
$getOldTimezone = api_get_timezone();
|
||
![]()
9 years ago
|
$isoCode = api_get_language_isocode();
|
||
![]()
6 years ago
|
if ($isoCode === 'pt') {
|
||
![]()
9 years ago
|
$isoCode = 'pt-BR';
|
||
|
}
|
||
![]()
6 years ago
|
$isoCode = ucfirst($isoCode);
|
||
|
$class = "Westsworld\TimeAgo\Translations\\".$isoCode;
|
||
|
|
||
|
if (class_exists($class)) {
|
||
|
$language = new $class();
|
||
|
} else {
|
||
|
$language = new Westsworld\TimeAgo\Translations\En();
|
||
![]()
9 years ago
|
}
|
||
![]()
6 years ago
|
$timeAgo = new TimeAgo($language);
|
||
|
$date = api_get_utc_datetime($date, null, true);
|
||
![]()
9 years ago
|
$value = $timeAgo->inWords($date);
|
||
|
date_default_timezone_set($getOldTimezone);
|
||
![]()
15 years ago
|
|
||
![]()
7 years ago
|
if ($returnDateDifference) {
|
||
|
$value = $timeAgo->dateDifference($date);
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
return $value;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Converts a date to the right timezone and localizes it in the format given as an argument.
|
||
|
*
|
||
![]()
16 years ago
|
* @param mixed The time to be converted
|
||
![]()
15 years ago
|
* @param mixed Format to be used (TIME_NO_SEC_FORMAT, DATE_FORMAT_SHORT, DATE_FORMAT_LONG, DATE_TIME_FORMAT_LONG)
|
||
![]()
16 years ago
|
* @param string Timezone to be converted from. If null, UTC will be assumed.
|
||
![]()
8 years ago
|
*
|
||
![]()
16 years ago
|
* @return string Converted and localized date
|
||
![]()
16 years ago
|
*
|
||
![]()
16 years ago
|
* @author Guillaume Viguier <guillaume.viguier@beeznest.com>
|
||
|
*/
|
||
![]()
9 years ago
|
function api_convert_and_format_date($time = null, $format = null, $from_timezone = null)
|
||
|
{
|
||
![]()
15 years ago
|
// First, convert the datetime to the right timezone
|
||
![]()
8 years ago
|
$time = api_get_local_time($time, null, $from_timezone, true);
|
||
![]()
15 years ago
|
// Second, localize the date
|
||
|
return api_format_date($time, $format);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
|
* Returns an array of translated week days in short names.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $language (optional) Language id. If it is omitted, the current interface language is assumed.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @return string Returns an array of week days (short names).
|
||
![]()
8 years ago
|
* Example: api_get_week_days_short('english') means array('Sun', 'Mon', ... 'Sat').
|
||
|
* Note: For all languges returned days are in the English order.
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_get_week_days_short($language = null)
|
||
|
{
|
||
![]()
15 years ago
|
$days = &_api_get_day_month_names($language);
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $days['days_short'];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns an array of translated week days.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $language (optional) Language id. If it is omitted,
|
||
![]()
8 years ago
|
* the current interface language is assumed.
|
||
|
*
|
||
![]()
8 years ago
|
* @return string Returns an array of week days.
|
||
![]()
8 years ago
|
* Example: api_get_week_days_long('english') means array('Sunday, 'Monday', ... 'Saturday').
|
||
|
* Note: For all languges returned days are in the English order.
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_get_week_days_long($language = null)
|
||
|
{
|
||
![]()
15 years ago
|
$days = &_api_get_day_month_names($language);
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $days['days_long'];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns an array of translated months in short names.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $language (optional) Language id.
|
||
![]()
8 years ago
|
* If it is omitted, the current interface language is assumed.
|
||
|
*
|
||
![]()
8 years ago
|
* @return string Returns an array of months (short names).
|
||
![]()
8 years ago
|
* Example: api_get_months_short('english') means array('Jan', 'Feb', ... 'Dec').
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_get_months_short($language = null)
|
||
|
{
|
||
![]()
15 years ago
|
$months = &_api_get_day_month_names($language);
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $months['months_short'];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns an array of translated months.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $language (optional) Language id.
|
||
![]()
8 years ago
|
* If it is omitted, the current interface language is assumed.
|
||
|
*
|
||
|
* @return string Returns an array of months.
|
||
|
* Example: api_get_months_long('english') means array('January, 'February' ... 'December').
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_get_months_long($language = null)
|
||
|
{
|
||
![]()
15 years ago
|
$months = &_api_get_day_month_names($language);
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $months['months_long'];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Name order conventions.
|
||
![]()
16 years ago
|
*/
|
||
|
|
||
|
/**
|
||
|
* Builds a person (full) name depending on the convention for a given language.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $first_name the first name of the person
|
||
|
* @param string $last_name the last name of the person
|
||
|
* @param string $title the title of the person
|
||
|
* @param int|string $format (optional) The person name format.
|
||
|
* It may be a pattern-string (for example '%t %l, %f' or '%T %F %L', ...) or
|
||
|
* some of the constants
|
||
|
* PERSON_NAME_COMMON_CONVENTION (default),
|
||
|
* PERSON_NAME_WESTERN_ORDER,
|
||
|
* PERSON_NAME_EASTERN_ORDER,
|
||
|
* PERSON_NAME_LIBRARY_ORDER.
|
||
|
* @param string $language (optional)
|
||
|
* The language id. If it is omitted, the current interface language is assumed.
|
||
|
* This parameter has meaning with the format PERSON_NAME_COMMON_CONVENTION only.
|
||
![]()
8 years ago
|
* @param string $username
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @return string The result is sort of full name of the person.
|
||
![]()
8 years ago
|
* Sample results:
|
||
|
* Peter Ustinoff or Dr. Peter Ustinoff - the Western order
|
||
|
* Ustinoff Peter or Dr. Ustinoff Peter - the Eastern order
|
||
|
* Ustinoff, Peter or - Dr. Ustinoff, Peter - the library order
|
||
|
* Note: See the file main/inc/lib/internationalization_database/name_order_conventions.php
|
||
|
* where you can check the convention for your language.
|
||
|
*
|
||
![]()
16 years ago
|
* @author Carlos Vargas <carlos.vargas@dokeos.com> - initial implementation.
|
||
|
* @author Ivan Tcholakov
|
||
|
*/
|
||
![]()
11 years ago
|
function api_get_person_name(
|
||
|
$first_name,
|
||
|
$last_name,
|
||
|
$title = null,
|
||
|
$format = null,
|
||
|
$language = null,
|
||
|
$username = null
|
||
|
) {
|
||
![]()
8 years ago
|
static $valid = [];
|
||
![]()
15 years ago
|
if (empty($format)) {
|
||
|
$format = PERSON_NAME_COMMON_CONVENTION;
|
||
|
}
|
||
![]()
8 years ago
|
// We check if the language is supported, otherwise we check the
|
||
|
// interface language of the parent language of sublanguage
|
||
![]()
11 years ago
|
if (empty($language)) {
|
||
![]()
9 years ago
|
// Do not set $setParentLanguageName because this function is called before
|
||
|
// the main language is loaded in global.inc.php
|
||
|
$language = api_get_interface_language(false, true, false);
|
||
![]()
15 years ago
|
}
|
||
![]()
11 years ago
|
|
||
![]()
15 years ago
|
if (!isset($valid[$format][$language])) {
|
||
|
if (is_int($format)) {
|
||
|
switch ($format) {
|
||
|
case PERSON_NAME_COMMON_CONVENTION:
|
||
|
$valid[$format][$language] = _api_get_person_name_convention($language, 'format');
|
||
![]()
12 years ago
|
$usernameOrderFromDatabase = api_get_setting('user_name_order');
|
||
|
if (isset($usernameOrderFromDatabase) && !empty($usernameOrderFromDatabase)) {
|
||
|
$valid[$format][$language] = $usernameOrderFromDatabase;
|
||
|
}
|
||
![]()
15 years ago
|
break;
|
||
|
case PERSON_NAME_WESTERN_ORDER:
|
||
|
$valid[$format][$language] = '%t %f %l';
|
||
|
break;
|
||
|
case PERSON_NAME_EASTERN_ORDER:
|
||
|
$valid[$format][$language] = '%t %l %f';
|
||
|
break;
|
||
|
case PERSON_NAME_LIBRARY_ORDER:
|
||
|
$valid[$format][$language] = '%t %l, %f';
|
||
|
break;
|
||
|
default:
|
||
|
$valid[$format][$language] = '%t %f %l';
|
||
|
break;
|
||
|
}
|
||
|
} else {
|
||
|
$valid[$format][$language] = _api_validate_person_name_format($format);
|
||
|
}
|
||
|
}
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
$format = $valid[$format][$language];
|
||
![]()
12 years ago
|
|
||
![]()
8 years ago
|
$keywords = [
|
||
![]()
10 years ago
|
'%firstname',
|
||
|
'%f',
|
||
|
'%F',
|
||
|
'%lastname',
|
||
|
'%l',
|
||
|
'%L',
|
||
|
'%title',
|
||
|
'%t',
|
||
|
'%T',
|
||
|
'%username',
|
||
|
'%u',
|
||
|
'%U',
|
||
![]()
8 years ago
|
];
|
||
![]()
12 years ago
|
|
||
![]()
8 years ago
|
$values = [
|
||
![]()
12 years ago
|
$first_name,
|
||
|
$first_name,
|
||
![]()
11 years ago
|
api_strtoupper($first_name),
|
||
![]()
12 years ago
|
$last_name,
|
||
|
$last_name,
|
||
![]()
11 years ago
|
api_strtoupper($last_name),
|
||
![]()
12 years ago
|
$title,
|
||
|
$title,
|
||
![]()
11 years ago
|
api_strtoupper($title),
|
||
![]()
12 years ago
|
$username,
|
||
|
$username,
|
||
![]()
11 years ago
|
api_strtoupper($username),
|
||
![]()
8 years ago
|
];
|
||
![]()
12 years ago
|
$person_name = str_replace($keywords, $values, $format);
|
||
![]()
10 years ago
|
|
||
![]()
15 years ago
|
return _api_clean_person_name($person_name);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks whether a given format represents person name in Western order (for which first name is first).
|
||
![]()
8 years ago
|
*
|
||
|
* @param int|string $format (optional) The person name format.
|
||
|
* It may be a pattern-string (for example '%t. %l, %f') or some of the constants
|
||
|
* PERSON_NAME_COMMON_CONVENTION (default),
|
||
|
* PERSON_NAME_WESTERN_ORDER,
|
||
|
* PERSON_NAME_EASTERN_ORDER,
|
||
|
* PERSON_NAME_LIBRARY_ORDER.
|
||
|
* @param string $language (optional) The language id. If it is omitted,
|
||
|
* the current interface language is assumed. This parameter has meaning with the
|
||
|
* format PERSON_NAME_COMMON_CONVENTION only.
|
||
|
*
|
||
![]()
8 years ago
|
* @return bool The result TRUE means that the order is first_name last_name,
|
||
![]()
8 years ago
|
* FALSE means last_name first_name.
|
||
|
* Note: You may use this function for determining the order of the fields or
|
||
|
* columns "First name" and "Last name" in forms, tables and reports.
|
||
|
*
|
||
![]()
16 years ago
|
* @author Ivan Tcholakov
|
||
|
*/
|
||
![]()
10 years ago
|
function api_is_western_name_order($format = null, $language = null)
|
||
|
{
|
||
![]()
8 years ago
|
static $order = [];
|
||
![]()
15 years ago
|
if (empty($format)) {
|
||
|
$format = PERSON_NAME_COMMON_CONVENTION;
|
||
|
}
|
||
![]()
13 years ago
|
|
||
![]()
11 years ago
|
if (empty($language)) {
|
||
![]()
13 years ago
|
$language = api_get_interface_language(false, true);
|
||
![]()
15 years ago
|
}
|
||
|
if (!isset($order[$format][$language])) {
|
||
|
$test_name = api_get_person_name('%f', '%l', '%t', $format, $language);
|
||
|
$order[$format][$language] = stripos($test_name, '%f') <= stripos($test_name, '%l');
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $order[$format][$language];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Returns a directive for sorting person names depending on a given language
|
||
|
* and based on the options in the internationalization "database".
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $language (optional) The input language.
|
||
![]()
8 years ago
|
* If it is omitted, the current interface language is assumed.
|
||
|
*
|
||
![]()
8 years ago
|
* @return bool Returns boolean value. TRUE means ORDER BY first_name, last_name
|
||
![]()
8 years ago
|
* FALSE means ORDER BY last_name, first_name.
|
||
|
* Note: You may use this function:
|
||
|
* 2. for constructing the ORDER clause of SQL queries, related to first_name and last_name;
|
||
|
* 3. for adjusting php-implemented sorting in tables and reports.
|
||
|
*
|
||
![]()
16 years ago
|
* @author Ivan Tcholakov
|
||
|
*/
|
||
![]()
8 years ago
|
function api_sort_by_first_name($language = null)
|
||
|
{
|
||
![]()
8 years ago
|
static $sort_by_first_name = [];
|
||
![]()
13 years ago
|
|
||
![]()
11 years ago
|
if (empty($language)) {
|
||
![]()
13 years ago
|
$language = api_get_interface_language(false, true);
|
||
![]()
15 years ago
|
}
|
||
|
if (!isset($sort_by_first_name[$language])) {
|
||
|
$sort_by_first_name[$language] = _api_get_person_name_convention($language, 'sort_by');
|
||
|
}
|
||
![]()
12 years ago
|
|
||
![]()
15 years ago
|
return $sort_by_first_name[$language];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Multibyte string conversion functions.
|
||
![]()
16 years ago
|
*/
|
||
|
|
||
|
/**
|
||
|
* Converts character encoding of a given string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being converted
|
||
|
* @param string $to_encoding the encoding that $string is being converted to
|
||
![]()
9 years ago
|
* @param string $from_encoding (optional) The encoding that $string is being converted from.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set is assumed.
|
||
|
*
|
||
![]()
9 years ago
|
* @return string Returns the converted string.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function mb_convert_encoding() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.mb-convert-encoding
|
||
![]()
16 years ago
|
*/
|
||
![]()
10 years ago
|
function api_convert_encoding($string, $to_encoding, $from_encoding = 'UTF-8')
|
||
![]()
11 years ago
|
{
|
||
![]()
9 years ago
|
if (strtoupper($to_encoding) === strtoupper($from_encoding)) {
|
||
|
return $string;
|
||
|
}
|
||
|
|
||
![]()
11 years ago
|
return mb_convert_encoding($string, $to_encoding, $from_encoding);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Converts a given string into UTF-8 encoded string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being converted
|
||
![]()
9 years ago
|
* @param string $from_encoding (optional) The encoding that $string is being converted from.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set is assumed.
|
||
|
*
|
||
![]()
9 years ago
|
* @return string Returns the converted string.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function utf8_encode() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.utf8-encode
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_utf8_encode($string, $from_encoding = 'UTF-8')
|
||
![]()
11 years ago
|
{
|
||
![]()
11 years ago
|
return mb_convert_encoding($string, 'UTF-8', $from_encoding);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Converts a given string from UTF-8 encoding to a specified encoding.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being converted
|
||
![]()
9 years ago
|
* @param string $to_encoding (optional) The encoding that $string is being converted to.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set is assumed.
|
||
|
*
|
||
|
* @return string Returns the converted string.
|
||
|
* This function is aimed at replacing the function utf8_decode() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.utf8-decode
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_utf8_decode($string, $to_encoding = null)
|
||
|
{
|
||
![]()
11 years ago
|
return mb_convert_encoding($string, $to_encoding, 'UTF-8');
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
7 years ago
|
* Converts a given string into the system encoding (or platform character set).
|
||
|
* When $from encoding is omitted on UTF-8 platforms then language dependent encoding
|
||
|
* is guessed/assumed. On non-UTF-8 platforms omitted $from encoding is assumed as UTF-8.
|
||
![]()
16 years ago
|
* When the parameter $check_utf8_validity is true the function checks string's
|
||
|
* UTF-8 validity and decides whether to try to convert it or not.
|
||
|
* This function is useful for problem detection or making workarounds.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being converted
|
||
|
* @param string $from_encoding (optional) The encoding that $string is being converted from.
|
||
|
* It is guessed when it is omitted.
|
||
|
* @param bool $check_utf8_validity (optional) A flag for UTF-8 validity check as condition for making conversion
|
||
|
*
|
||
|
* @return string returns the converted string
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_to_system_encoding($string, $from_encoding = null, $check_utf8_validity = false)
|
||
|
{
|
||
![]()
15 years ago
|
$system_encoding = api_get_system_encoding();
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return api_convert_encoding($string, $system_encoding, $from_encoding);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Converts all applicable characters to HTML entities.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input string
|
||
|
* @param int $quote_style (optional) The quote style - ENT_COMPAT (default), ENT_QUOTES, ENT_NOQUOTES
|
||
|
* @param string $encoding (optional) The encoding (of the input string) used in conversion.
|
||
|
* If it is omitted, the platform character set is assumed.
|
||
|
*
|
||
![]()
9 years ago
|
* @return string Returns the converted string.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function htmlentities() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.htmlentities
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_htmlentities($string, $quote_style = ENT_COMPAT, $encoding = 'UTF-8')
|
||
|
{
|
||
![]()
10 years ago
|
switch ($quote_style) {
|
||
![]()
15 years ago
|
case ENT_COMPAT:
|
||
![]()
8 years ago
|
$string = str_replace(['&', '"', '<', '>'], ['&', '"', '<', '>'], $string);
|
||
![]()
15 years ago
|
break;
|
||
|
case ENT_QUOTES:
|
||
![]()
8 years ago
|
$string = str_replace(['&', '\'', '"', '<', '>'], ['&', ''', '"', '<', '>'], $string);
|
||
![]()
15 years ago
|
break;
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
return mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
12 years ago
|
* Converts HTML entities into normal characters.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input string
|
||
|
* @param int $quote_style (optional) The quote style - ENT_COMPAT (default), ENT_QUOTES, ENT_NOQUOTES
|
||
|
* @param string $encoding (optional) The encoding (of the result) used in conversion.
|
||
|
* If it is omitted, the platform character set is assumed.
|
||
|
*
|
||
![]()
9 years ago
|
* @return string Returns the converted string.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function html_entity_decode() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/html_entity_decode
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_html_entity_decode($string, $quote_style = ENT_COMPAT, $encoding = 'UTF-8')
|
||
|
{
|
||
![]()
15 years ago
|
if (empty($encoding)) {
|
||
|
$encoding = _api_mb_internal_encoding();
|
||
|
}
|
||
|
if (api_is_encoding_supported($encoding)) {
|
||
|
if (!api_is_utf8($encoding)) {
|
||
|
$string = api_utf8_encode($string, $encoding);
|
||
|
}
|
||
|
$string = html_entity_decode($string, $quote_style, 'UTF-8');
|
||
|
if (!api_is_utf8($encoding)) {
|
||
|
return api_utf8_decode($string, $encoding);
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $string;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
12 years ago
|
return $string; // Here the function gives up.
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* This function encodes (conditionally) a given string to UTF-8 if XmlHttp-request has been detected.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being converted
|
||
![]()
9 years ago
|
* @param string $from_encoding (optional) The encoding that $string is being converted from.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set is assumed.
|
||
|
*
|
||
|
* @return string returns the converted string
|
||
![]()
16 years ago
|
*/
|
||
![]()
10 years ago
|
function api_xml_http_response_encode($string, $from_encoding = 'UTF8')
|
||
|
{
|
||
![]()
15 years ago
|
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
|
||
|
if (empty($from_encoding)) {
|
||
|
$from_encoding = _api_mb_internal_encoding();
|
||
|
}
|
||
|
if (!api_is_utf8($from_encoding)) {
|
||
|
return api_utf8_encode($string, $from_encoding);
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $string;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Transliterates a string with arbitrary encoding into a plain ASCII string.
|
||
|
*
|
||
|
* Example:
|
||
|
* echo api_transliterate(api_html_entity_decode(
|
||
![]()
9 years ago
|
* 'Фёдор '.
|
||
|
* 'Михайлович '.
|
||
|
* 'Достоевкий',
|
||
|
* ENT_QUOTES, 'UTF-8'), 'X', 'UTF-8');
|
||
![]()
16 years ago
|
* The output should be: Fyodor Mihaylovich Dostoevkiy
|
||
|
*
|
||
![]()
8 years ago
|
* @param string $string the input string
|
||
|
* @param string $unknown (optional) Replacement character for unknown characters and illegal UTF-8 sequences
|
||
![]()
8 years ago
|
* @param string $from_encoding (optional) The encoding of the input string.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set is assumed.
|
||
![]()
16 years ago
|
*
|
||
![]()
8 years ago
|
* @return string plain ASCII output
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_transliterate($string, $unknown = '?', $from_encoding = null)
|
||
|
{
|
||
|
return URLify::transliterate($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
|
* Takes the first character in a string and returns its Unicode codepoint.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $character the input string
|
||
|
* @param string $encoding (optional) The encoding of the input string.
|
||
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
![]()
9 years ago
|
* @return int Returns: the codepoint of the first character; or 0xFFFD (unknown character) when the input string is empty.
|
||
![]()
8 years ago
|
* This is a multibyte aware version of the function ord().
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.ord.php
|
||
![]()
16 years ago
|
* Note the difference with the original funtion ord(): ord('') returns 0, api_ord('') returns 0xFFFD (unknown character).
|
||
|
*/
|
||
![]()
9 years ago
|
function api_ord($character, $encoding = 'UTF-8')
|
||
|
{
|
||
![]()
9 years ago
|
return Utf8::ord(api_utf8_encode($character, $encoding));
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
8 years ago
|
* This function returns a string or an array with all occurrences of search
|
||
|
* in subject (ignoring case) replaced with the given replace value.
|
||
![]()
8 years ago
|
*
|
||
|
* @param mixed $search string or array of strings to be found
|
||
|
* @param mixed $replace string or array of strings used for replacement
|
||
|
* @param mixed $subject string or array of strings being searched
|
||
|
* @param int $count (optional) The number of matched and replaced needles
|
||
|
* will be returned in count, which is passed by reference
|
||
![]()
9 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
![]()
9 years ago
|
* @return mixed String or array as a result.
|
||
![]()
8 years ago
|
* Notes:
|
||
|
* If $subject is an array, then the search and replace is performed with
|
||
|
* every entry of subject, the return value is an array.
|
||
|
* If $search and $replace are arrays, then the function takes a value from
|
||
|
* each array and uses it to do search and replace on subject.
|
||
|
* If $replace has fewer values than search, then an empty string is used for the rest of replacement values.
|
||
|
* If $search is an array and $replace is a string, then this replacement string is used for every value of search.
|
||
|
* This function is aimed at replacing the function str_ireplace() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.str-ireplace
|
||
|
*
|
||
![]()
16 years ago
|
* @author Henri Sivonen, mailto:hsivonen@iki.fi
|
||
![]()
8 years ago
|
*
|
||
|
* @see http://hsivonen.iki.fi/php-utf8/
|
||
![]()
16 years ago
|
* Adaptation for Chamilo 1.8.7, 2010
|
||
|
* Initial implementation Dokeos LMS, August 2009
|
||
![]()
8 years ago
|
*
|
||
![]()
16 years ago
|
* @author Ivan Tcholakov
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_str_ireplace($search, $replace, $subject, &$count = null, $encoding = null)
|
||
![]()
11 years ago
|
{
|
||
|
return Utf8::str_ireplace($search, $replace, $subject, $count);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Converts a string to an array.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input string
|
||
|
* @param int $split_length maximum character-length of the chunk, one character by default
|
||
|
* @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.
|
||
|
*
|
||
![]()
9 years ago
|
* @return array The result array of chunks with the spcified length.
|
||
![]()
8 years ago
|
* Notes:
|
||
|
* If the optional split_length parameter is specified, the returned array will be broken down into chunks
|
||
|
* with each being split_length in length, otherwise each chunk will be one character in length.
|
||
|
* FALSE is returned if split_length is less than 1.
|
||
|
* If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.
|
||
|
* This function is aimed at replacing the function str_split() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/str_split
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_str_split($string, $split_length = 1, $encoding = null)
|
||
|
{
|
||
|
return Utf8::str_split($string, $split_length);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds position of first occurrence of a string within another, case insensitive.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string from which to get the position of the first occurrence
|
||
|
* @param string $needle the string to be found
|
||
|
* @param int $offset The position in $haystack to start searching from.
|
||
|
* If it is omitted, searching starts from the beginning.
|
||
![]()
8 years ago
|
* @param string $encoding (optional) The used internally by this function
|
||
![]()
8 years ago
|
* character encoding. If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
![]()
8 years ago
|
* @return mixed Returns the numeric position of the first occurrence of
|
||
![]()
8 years ago
|
* $needle in the $haystack, or FALSE if $needle is not found.
|
||
|
* Note: The first character's position is 0, the second character position is 1, and so on.
|
||
|
* This function is aimed at replacing the functions stripos() and mb_stripos() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.stripos
|
||
|
* @see http://php.net/manual/en/function.mb-stripos
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_stripos($haystack, $needle, $offset = 0, $encoding = null)
|
||
|
{
|
||
|
return Utf8::stripos($haystack, $needle, $offset);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds first occurrence of a string within another, case insensitive.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string from which to get the first occurrence
|
||
|
* @param mixed $needle the string to be found
|
||
|
* @param bool $before_needle (optional) Determines which portion of $haystack
|
||
|
* this function returns. The default value is FALSE.
|
||
|
* @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.
|
||
|
*
|
||
![]()
9 years ago
|
* @return mixed Returns the portion of $haystack, or FALSE if $needle is not found.
|
||
![]()
8 years ago
|
* Notes:
|
||
|
* If $needle is not a string, it is converted to an integer and applied as the
|
||
|
* ordinal value (codepoint if the encoding is UTF-8) of a character.
|
||
|
* If $before_needle is set to TRUE, the function returns all of $haystack
|
||
|
* from the beginning to the first occurrence of $needle.
|
||
|
* If $before_needle is set to FALSE, the function returns all of $haystack f
|
||
|
* rom the first occurrence of $needle to the end.
|
||
|
* This function is aimed at replacing the functions stristr() and mb_stristr() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.stristr
|
||
|
* @see http://php.net/manual/en/function.mb-stristr
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_stristr($haystack, $needle, $before_needle = false, $encoding = null)
|
||
|
{
|
||
|
return Utf8::stristr($haystack, $needle, $before_needle);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns length of the input string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string which length is to be calculated
|
||
![]()
9 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @return int Returns the number of characters within the string. A multi-byte character is counted as 1.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the functions strlen() and mb_strlen() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strlen
|
||
|
* @see http://php.net/manual/en/function.mb-strlen
|
||
![]()
16 years ago
|
* Note: When you use strlen() to test for an empty string, you needn't change it to api_strlen().
|
||
|
* For example, in lines like the following:
|
||
|
* if (strlen($string) > 0)
|
||
|
* if (strlen($string) != 0)
|
||
|
* there is no need the original function strlen() to be changed, it works correctly and faster for these cases.
|
||
|
*/
|
||
![]()
11 years ago
|
function api_strlen($string, $encoding = null)
|
||
|
{
|
||
|
return Utf8::strlen($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds position of first occurrence of a string within another.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string from which to get the position of the first occurrence
|
||
|
* @param string $needle the string to be found
|
||
|
* @param int $offset (optional) The position in $haystack to start searching from. If it is omitted, searching starts from the beginning.
|
||
![]()
9 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @return mixed Returns the numeric position of the first occurrence of $needle in the $haystack, or FALSE if $needle is not found.
|
||
![]()
8 years ago
|
* Note: The first character's position is 0, the second character position is 1, and so on.
|
||
|
* This function is aimed at replacing the functions strpos() and mb_strpos() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strpos
|
||
|
* @see http://php.net/manual/en/function.mb-strpos
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strpos($haystack, $needle, $offset = 0, $encoding = null)
|
||
|
{
|
||
|
return Utf8::strpos($haystack, $needle, $offset);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds the last occurrence of a character in a string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string from which to get the last occurrence
|
||
|
* @param mixed $needle the string which first character is to be found
|
||
|
* @param bool $before_needle (optional) Determines which portion of $haystack this function returns. The default value is FALSE.
|
||
|
* @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.
|
||
|
*
|
||
![]()
9 years ago
|
* @return mixed Returns the portion of $haystack, or FALSE if the first character from $needle is not found.
|
||
![]()
8 years ago
|
* Notes:
|
||
|
* If $needle is not a string, it is converted to an integer and applied as the ordinal value (codepoint if the encoding is UTF-8) of a character.
|
||
|
* If $before_needle is set to TRUE, the function returns all of $haystack from the beginning to the first occurrence.
|
||
|
* If $before_needle is set to FALSE, the function returns all of $haystack from the first occurrence to the end.
|
||
|
* This function is aimed at replacing the functions strrchr() and mb_strrchr() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strrchr
|
||
|
* @see http://php.net/manual/en/function.mb-strrchr
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strrchr($haystack, $needle, $before_needle = false, $encoding = null)
|
||
|
{
|
||
|
return Utf8::strrchr($haystack, $needle);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Reverses a string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string to be reversed
|
||
![]()
9 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @return string Returns the reversed string.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function strrev() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strrev
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strrev($string, $encoding = null)
|
||
|
{
|
||
|
return Utf8::strrev($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds the position of last occurrence (case insensitive) of a string in a string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string from which to get the position of the last occurrence
|
||
|
* @param string $needle the string to be found
|
||
|
* @param int $offset (optional) $offset may be specified to begin searching an arbitrary position. Negative values will stop searching at an arbitrary point prior to the end of the string.
|
||
![]()
9 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @return mixed Returns the numeric position of the first occurrence (case insensitive) of $needle in the $haystack, or FALSE if $needle is not found.
|
||
![]()
8 years ago
|
* Note: The first character's position is 0, the second character position is 1, and so on.
|
||
|
* This function is aimed at replacing the functions strripos() and mb_strripos() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strripos
|
||
|
* @see http://php.net/manual/en/function.mb-strripos
|
||
![]()
16 years ago
|
*/
|
||
![]()
12 years ago
|
function api_strripos($haystack, $needle, $offset = 0, $encoding = null)
|
||
|
{
|
||
![]()
11 years ago
|
return Utf8::strripos($haystack, $needle, $offset);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds the position of last occurrence of a string in a string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string from which to get the position of the last occurrence
|
||
|
* @param string $needle the string to be found
|
||
|
* @param int $offset (optional) $offset may be specified to begin searching an arbitrary position. Negative values will stop searching at an arbitrary point prior to the end of the string.
|
||
![]()
9 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @return mixed Returns the numeric position of the first occurrence of $needle in the $haystack, or FALSE if $needle is not found.
|
||
![]()
8 years ago
|
* Note: The first character's position is 0, the second character position is 1, and so on.
|
||
|
* This function is aimed at replacing the functions strrpos() and mb_strrpos() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strrpos
|
||
|
* @see http://php.net/manual/en/function.mb-strrpos
|
||
![]()
16 years ago
|
*/
|
||
![]()
12 years ago
|
function api_strrpos($haystack, $needle, $offset = 0, $encoding = null)
|
||
|
{
|
||
![]()
11 years ago
|
return Utf8::strrpos($haystack, $needle, $offset);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Finds first occurrence of a string within another.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string from which to get the first occurrence
|
||
|
* @param mixed $needle the string to be found
|
||
|
* @param bool $before_needle (optional) Determines which portion of $haystack this function returns. The default value is FALSE.
|
||
|
* @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.
|
||
|
*
|
||
![]()
9 years ago
|
* @return mixed Returns the portion of $haystack, or FALSE if $needle is not found.
|
||
![]()
8 years ago
|
* Notes:
|
||
|
* If $needle is not a string, it is converted to an integer and applied as the ordinal value (codepoint if the encoding is UTF-8) of a character.
|
||
|
* If $before_needle is set to TRUE, the function returns all of $haystack from the beginning to the first occurrence of $needle.
|
||
|
* If $before_needle is set to FALSE, the function returns all of $haystack from the first occurrence of $needle to the end.
|
||
|
* This function is aimed at replacing the functions strstr() and mb_strstr() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strstr
|
||
|
* @see http://php.net/manual/en/function.mb-strstr
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strstr($haystack, $needle, $before_needle = false, $encoding = null)
|
||
|
{
|
||
|
return Utf8::strstr($haystack, $needle, $before_needle);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Makes a string lowercase.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being lowercased
|
||
![]()
16 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
|
* @return string Returns the string with all alphabetic characters converted to lowercase.
|
||
|
* This function is aimed at replacing the functions strtolower() and mb_strtolower() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strtolower
|
||
|
* @see http://php.net/manual/en/function.mb-strtolower
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strtolower($string, $encoding = null)
|
||
|
{
|
||
|
return Utf8::strtolower($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Makes a string uppercase.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being uppercased
|
||
![]()
16 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
|
* @return string Returns the string with all alphabetic characters converted to uppercase.
|
||
|
* This function is aimed at replacing the functions strtoupper() and mb_strtoupper() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strtoupper
|
||
|
* @see http://php.net/manual/en/function.mb-strtoupper
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strtoupper($string, $encoding = null)
|
||
|
{
|
||
|
return Utf8::strtoupper($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* // Gets part of a string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input string
|
||
|
* @param int $start the first position from which the extracted part begins
|
||
|
* @param int $length the length in character of the extracted part
|
||
![]()
8 years ago
|
* @param string $encoding (optional) The used internally by this function
|
||
![]()
8 years ago
|
* character encoding. If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
![]()
8 years ago
|
* @return string Returns the part of the string specified by the start and length parameters.
|
||
![]()
8 years ago
|
* Note: First character's position is 0. Second character position is 1, and so on.
|
||
|
* This function is aimed at replacing the functions substr() and mb_substr() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.substr
|
||
|
* @see http://php.net/manual/en/function.mb-substr
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_substr($string, $start, $length = null, $encoding = null)
|
||
|
{
|
||
![]()
10 years ago
|
if (is_null($length)) {
|
||
|
$length = api_strlen($string, $encoding);
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return Utf8::substr($string, $start, $length);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
|
* Counts the number of substring occurrences.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $haystack the string being checked
|
||
|
* @param string $needle the string being found
|
||
![]()
8 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
|
* @return int the number of times the needle substring occurs in the haystack string
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.mb-substr-count.php
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_substr_count($haystack, $needle, $encoding = null)
|
||
|
{
|
||
|
return Utf8::substr_count($haystack, $needle);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
|
* Replaces text within a portion of a string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input string
|
||
|
* @param string $replacement the replacement string
|
||
|
* @param int $start The position from which replacing will begin.
|
||
|
* Notes:
|
||
|
* If $start is positive, the replacing will begin at the $start'th offset into the string.
|
||
|
* If $start is negative, the replacing will begin at the $start'th character from the end of the string.
|
||
|
* @param int $length (optional) The position where replacing will end.
|
||
|
* Notes:
|
||
|
* If given and is positive, it represents the length of the portion of the string which is to be replaced.
|
||
|
* If it is negative, it represents the number of characters from the end of string at which to stop replacing.
|
||
|
* If it is not given, then it will default to api_strlen($string); i.e. end the replacing at the end of string.
|
||
|
* If $length is zero, then this function will have the effect of inserting replacement into the string at the given start offset.
|
||
|
* @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.
|
||
|
*
|
||
![]()
8 years ago
|
* @return string The result string is returned.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function substr_replace() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/function.substr-replace
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_substr_replace($string, $replacement, $start, $length = null, $encoding = null)
|
||
|
{
|
||
![]()
10 years ago
|
if (is_null($length)) {
|
||
|
$length = api_strlen($string);
|
||
|
}
|
||
|
|
||
![]()
11 years ago
|
return UTf8::substr_replace($string, $replacement, $start, $length);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Makes a string's first character uppercase.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input string
|
||
![]()
8 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
![]()
8 years ago
|
* @return string Returns a string with the first character capitalized, if that character is alphabetic.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function ucfirst() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.ucfirst
|
||
![]()
16 years ago
|
*/
|
||
![]()
10 years ago
|
function api_ucfirst($string, $encoding = null)
|
||
|
{
|
||
![]()
11 years ago
|
return Utf8::ucfirst($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Uppercases the first character of each word in a string.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input string
|
||
![]()
8 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
![]()
8 years ago
|
* @return string Returns the modified string.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function ucwords() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.ucwords
|
||
![]()
16 years ago
|
*/
|
||
![]()
10 years ago
|
function api_ucwords($string, $encoding = null)
|
||
|
{
|
||
![]()
11 years ago
|
return Utf8::ucwords($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Performs a regular expression match, UTF-8 aware when it is applicable.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $pattern the pattern to search for, as a string
|
||
|
* @param string $subject the input string
|
||
|
* @param array &$matches (optional) If matches is provided,
|
||
|
* then it is filled with the results of search (as an array).
|
||
|
* $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
|
||
|
* @param int $flags (optional) Could be PREG_OFFSET_CAPTURE. If this flag is passed, for every occurring match the appendant string offset will also be returned.
|
||
|
* Note that this changes the return value in an array where every element is an array consisting of the matched string at index 0 and its string offset into subject at index 1.
|
||
|
* @param int $offset (optional) Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search.
|
||
![]()
8 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
|
* @return int|bool returns the number of times pattern matches or FALSE if an error occurred
|
||
|
*
|
||
|
* @see http://php.net/preg_match
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_preg_match(
|
||
|
$pattern,
|
||
|
$subject,
|
||
|
&$matches = null,
|
||
|
$flags = 0,
|
||
|
$offset = 0,
|
||
|
$encoding = null
|
||
|
) {
|
||
![]()
15 years ago
|
if (empty($encoding)) {
|
||
|
$encoding = _api_mb_internal_encoding();
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return preg_match(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $matches, $flags, $offset);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Performs a global regular expression match, UTF-8 aware when it is applicable.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $pattern the pattern to search for, as a string
|
||
|
* @param string $subject the input string
|
||
|
* @param array &$matches (optional) Array of all matches in multi-dimensional array ordered according to $flags
|
||
|
* @param int $flags (optional) Can be a combination of the following flags (note that it doesn't make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):
|
||
|
* PREG_PATTERN_ORDER - orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on;
|
||
|
* PREG_SET_ORDER - orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on;
|
||
|
* PREG_OFFSET_CAPTURE - If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches
|
||
|
* in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
|
||
|
* If no order flag is given, PREG_PATTERN_ORDER is assumed.
|
||
|
* @param int $offset (optional) Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search.
|
||
![]()
16 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
|
* @return int|bool returns the number of full pattern matches (which might be zero), or FALSE if an error occurred
|
||
|
*
|
||
|
* @see http://php.net/preg_match_all
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_preg_match_all($pattern, $subject, &$matches, $flags = PREG_PATTERN_ORDER, $offset = 0, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
if (empty($encoding)) {
|
||
|
$encoding = _api_mb_internal_encoding();
|
||
|
}
|
||
|
if (is_null($flags)) {
|
||
|
$flags = PREG_PATTERN_ORDER;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return preg_match_all(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $matches, $flags, $offset);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Performs a regular expression search and replace, UTF-8 aware when it is applicable.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string|array $pattern The pattern to search for. It can be either a string or an array with strings.
|
||
|
* @param string|array $replacement the string or an array with strings to replace
|
||
|
* @param string|array $subject the string or an array with strings to search and replace
|
||
|
* @param int $limit The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).
|
||
|
* @param int &$count If specified, this variable will be filled with the number of replacements done
|
||
|
* @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 array|string|null returns an array if the subject parameter is an array, or a string otherwise.
|
||
|
* If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.
|
||
|
*
|
||
|
* @see http://php.net/preg_replace
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_preg_replace($pattern, $replacement, $subject, $limit = -1, &$count = 0, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
if (empty($encoding)) {
|
||
|
$encoding = _api_mb_internal_encoding();
|
||
|
}
|
||
|
$is_utf8 = api_is_utf8($encoding);
|
||
|
if (is_array($pattern)) {
|
||
|
foreach ($pattern as &$p) {
|
||
|
$p = $is_utf8 ? $p.'u' : $p;
|
||
|
}
|
||
|
} else {
|
||
|
$pattern = $is_utf8 ? $pattern.'u' : $pattern;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return preg_replace($pattern, $replacement, $subject, $limit, $count);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Splits a string by a regular expression, UTF-8 aware when it is applicable.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $pattern the pattern to search for, as a string
|
||
|
* @param string $subject the input string
|
||
|
* @param int $limit (optional) If specified, then only substrings up to $limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or null means "no limit" and, as is standard across PHP.
|
||
|
* @param int $flags (optional) $flags can be any combination of the following flags (combined with bitwise | operator):
|
||
|
* PREG_SPLIT_NO_EMPTY - if this flag is set, only non-empty pieces will be returned;
|
||
|
* PREG_SPLIT_DELIM_CAPTURE - if this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well;
|
||
|
* PREG_SPLIT_OFFSET_CAPTURE - If this flag is set, for every occurring match the appendant string offset will also be returned.
|
||
|
* Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
|
||
![]()
16 years ago
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
|
* @return array returns an array containing substrings of $subject split along boundaries matched by $pattern
|
||
|
*
|
||
|
* @see http://php.net/preg_split
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_preg_split($pattern, $subject, $limit = -1, $flags = 0, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
if (empty($encoding)) {
|
||
|
$encoding = _api_mb_internal_encoding();
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return preg_split(api_is_utf8($encoding) ? $pattern.'u' : $pattern, $subject, $limit, $flags);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* String comparison.
|
||
![]()
16 years ago
|
*/
|
||
|
|
||
|
/**
|
||
|
* Performs string comparison, case insensitive, language sensitive, with extended multibyte support.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string1 the first string
|
||
|
* @param string $string2 the second string
|
||
![]()
8 years ago
|
* @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
|
||
|
* @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.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
|
||
![]()
8 years ago
|
* This function is aimed at replacing the function strcasecmp() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strcasecmp
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function api_strcasecmp($string1, $string2, $language = null, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
return api_strcmp(api_strtolower($string1, $encoding), api_strtolower($string2, $encoding), $language, $encoding);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Performs string comparison, case sensitive, language sensitive, with extended multibyte support.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string1 the first string
|
||
|
* @param string $string2 the second string
|
||
![]()
16 years ago
|
* @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
|
||
![]()
10 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
|
* @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
|
||
|
* This function is aimed at replacing the function strcmp() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strcmp.php
|
||
|
* @see http://php.net/manual/en/collator.compare.php
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strcmp($string1, $string2, $language = null, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
return strcmp($string1, $string2);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Performs string comparison in so called "natural order", case sensitive, language sensitive, with extended multibyte support.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string1 the first string
|
||
|
* @param string $string2 the second string
|
||
![]()
16 years ago
|
* @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
|
||
![]()
10 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
|
* @return int Returns < 0 if $string1 is less than $string2; > 0 if $string1 is greater than $string2; and 0 if the strings are equal.
|
||
|
* This function is aimed at replacing the function strnatcmp() for human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.strnatcmp.php
|
||
|
* @see http://php.net/manual/en/collator.compare.php
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_strnatcmp($string1, $string2, $language = null, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
return strnatcmp($string1, $string2);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Sorting arrays.
|
||
![]()
16 years ago
|
*/
|
||
|
|
||
|
/**
|
||
|
* Sorts an array using natural order algorithm.
|
||
![]()
8 years ago
|
*
|
||
|
* @param array $array the input array
|
||
![]()
16 years ago
|
* @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
|
||
![]()
10 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
|
* @return bool Returns TRUE on success, FALSE on error.
|
||
|
* This function is aimed at replacing the function natsort() for sorting human-language strings.
|
||
|
*
|
||
|
* @see http://php.net/manual/en/function.natsort.php
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_natsort(&$array, $language = null, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
return natsort($array);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Sorts an array using natural order algorithm in reverse order.
|
||
![]()
8 years ago
|
*
|
||
|
* @param array $array the input array
|
||
![]()
16 years ago
|
* @param string $language (optional) The language in which comparison is to be made. If language is omitted, interface language is assumed then.
|
||
![]()
10 years ago
|
* @param string $encoding (optional) The used internally by this function character encoding.
|
||
![]()
8 years ago
|
* If it is omitted, the platform character set will be used by default.
|
||
|
*
|
||
|
* @return bool returns TRUE on success, FALSE on error
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_natrsort(&$array, $language = null, $encoding = null)
|
||
|
{
|
||
![]()
15 years ago
|
return uasort($array, '_api_strnatrcmp');
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Encoding management functions.
|
||
![]()
16 years ago
|
*/
|
||
|
|
||
|
/**
|
||
|
* This function unifies the encoding identificators, so they could be compared.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string|array $encoding the specified encoding
|
||
|
*
|
||
|
* @return string returns the encoding identificator modified in suitable for comparison way
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_refine_encoding_id($encoding)
|
||
|
{
|
||
![]()
9 years ago
|
if (is_array($encoding)) {
|
||
![]()
15 years ago
|
return array_map('api_refine_encoding_id', $encoding);
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return strtoupper(str_replace('_', '-', $encoding));
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* This function checks whether two $encoding are equal (same, equvalent).
|
||
![]()
8 years ago
|
*
|
||
|
* @param string|array $encoding1 The first encoding
|
||
|
* @param string|array $encoding2 The second encoding
|
||
|
* @param bool $strict When this parameter is TRUE the comparison ignores aliases of encodings.
|
||
|
* When the parameter is FALSE, aliases are taken into account.
|
||
|
*
|
||
|
* @return bool returns TRUE if the encodings are equal, FALSE otherwise
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_equal_encodings($encoding1, $encoding2, $strict = false)
|
||
|
{
|
||
![]()
8 years ago
|
static $equal_encodings = [];
|
||
![]()
15 years ago
|
if (is_array($encoding1)) {
|
||
|
foreach ($encoding1 as $encoding) {
|
||
|
if (api_equal_encodings($encoding, $encoding2, $strict)) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return false;
|
||
![]()
8 years ago
|
} elseif (is_array($encoding2)) {
|
||
![]()
15 years ago
|
foreach ($encoding2 as $encoding) {
|
||
|
if (api_equal_encodings($encoding1, $encoding, $strict)) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return false;
|
||
|
}
|
||
|
if (!isset($equal_encodings[$encoding1][$encoding2][$strict])) {
|
||
|
$encoding_1 = api_refine_encoding_id($encoding1);
|
||
|
$encoding_2 = api_refine_encoding_id($encoding2);
|
||
|
if ($encoding_1 == $encoding_2) {
|
||
|
$result = true;
|
||
|
} else {
|
||
|
if ($strict) {
|
||
|
$result = false;
|
||
|
} else {
|
||
|
$alias1 = _api_get_character_map_name($encoding_1);
|
||
|
$alias2 = _api_get_character_map_name($encoding_2);
|
||
|
$result = !empty($alias1) && !empty($alias2) && $alias1 == $alias2;
|
||
|
}
|
||
|
}
|
||
|
$equal_encodings[$encoding1][$encoding2][$strict] = $result;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $equal_encodings[$encoding1][$encoding2][$strict];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* This function checks whether a given encoding is UTF-8.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $encoding the tested encoding
|
||
|
*
|
||
|
* @return bool returns TRUE if the given encoding id means UTF-8, otherwise returns false
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_is_utf8($encoding)
|
||
|
{
|
||
![]()
8 years ago
|
static $result = [];
|
||
![]()
15 years ago
|
if (!isset($result[$encoding])) {
|
||
|
$result[$encoding] = api_equal_encodings($encoding, 'UTF-8');
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $result[$encoding];
|
||
![]()
16 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
16 years ago
|
/**
|
||
|
* This function returns the encoding, currently used by the system.
|
||
![]()
8 years ago
|
*
|
||
|
* @return string The system's encoding.
|
||
|
* Note: The value of api_get_setting('platform_charset') is tried to be returned first,
|
||
|
* on the second place the global variable $charset is tried to be returned. If for some
|
||
|
* reason both attempts fail, then the libraly's internal value will be returned.
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_get_system_encoding()
|
||
|
{
|
||
|
return 'UTF-8';
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks whether a specified encoding is supported by this API.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $encoding the specified encoding
|
||
|
*
|
||
|
* @return bool returns TRUE when the specified encoding is supported, FALSE othewise
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_is_encoding_supported($encoding)
|
||
|
{
|
||
![]()
8 years ago
|
static $supported = [];
|
||
![]()
15 years ago
|
if (!isset($supported[$encoding])) {
|
||
|
$supported[$encoding] = _api_mb_supports($encoding) || _api_iconv_supports($encoding) || _api_convert_encoding_supports($encoding);
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
15 years ago
|
return $supported[$encoding];
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
|
* Detects encoding of plain text.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the input text
|
||
|
* @param string $language (optional) The language of the input text, provided if it is known
|
||
|
*
|
||
|
* @return string returns the detected encoding
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_detect_encoding($string, $language = null)
|
||
|
{
|
||
![]()
15 years ago
|
// Testing against valid UTF-8 first.
|
||
|
if (api_is_valid_utf8($string)) {
|
||
|
return 'UTF-8';
|
||
|
}
|
||
![]()
11 years ago
|
|
||
|
return mb_detect_encoding($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
8 years ago
|
* String validation functions concerning certain encodings.
|
||
![]()
16 years ago
|
*/
|
||
|
|
||
|
/**
|
||
|
* Checks a string for UTF-8 validity.
|
||
![]()
8 years ago
|
*
|
||
![]()
9 years ago
|
* @param string $string
|
||
![]()
13 years ago
|
*
|
||
![]()
9 years ago
|
* @return string
|
||
![]()
16 years ago
|
*/
|
||
![]()
9 years ago
|
function api_is_valid_utf8($string)
|
||
![]()
11 years ago
|
{
|
||
![]()
11 years ago
|
return Utf8::isUtf8($string);
|
||
![]()
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks whether a string contains 7-bit ASCII characters only.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string to be tested/validated
|
||
|
*
|
||
|
* @return bool returns TRUE when the tested string contains 7-bit
|
||
|
* ASCII characters only, FALSE othewise
|
||
![]()
16 years ago
|
*/
|
||
![]()
11 years ago
|
function api_is_valid_ascii(&$string)
|
||
|
{
|
||
|
return mb_detect_encoding($string, 'ASCII', true) == 'ASCII' ? true : false;
|
||
![]()
16 years ago
|
}
|
||
|
|
||
![]()
12 years ago
|
/**
|
||
![]()
8 years ago
|
* Return true a date is valid.
|
||
|
*
|
||
|
* @param string $date example: 2014-06-30 13:05:05
|
||
![]()
12 years ago
|
* @param string $format example: "Y-m-d H:i:s"
|
||
|
*
|
||
|
* @return bool
|
||
![]()
12 years ago
|
*/
|
||
![]()
11 years ago
|
function api_is_valid_date($date, $format = 'Y-m-d H:i:s')
|
||
|
{
|
||
![]()
12 years ago
|
$d = DateTime::createFromFormat($format, $date);
|
||
![]()
8 years ago
|
|
||
![]()
12 years ago
|
return $d && $d->format($format) == $date;
|
||
![]()
12 years ago
|
}
|
||
![]()
11 years ago
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Returns the variable translated.
|
||
|
*
|
||
|
* @param string $variable the string to translate
|
||
![]()
11 years ago
|
* @param string $pluginName the Plugin name
|
||
![]()
8 years ago
|
*
|
||
![]()
11 years ago
|
* @return string the variable translated
|
||
|
*/
|
||
![]()
8 years ago
|
function get_plugin_lang($variable, $pluginName)
|
||
|
{
|
||
![]()
11 years ago
|
$plugin = $pluginName::create();
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return $plugin->get_lang($variable);
|
||
|
}
|
||
![]()
11 years ago
|
|
||
![]()
16 years ago
|
/**
|
||
![]()
11 years ago
|
* Returns an array of translated week days and months, short and normal names.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $language (optional Language id. If it is omitted,
|
||
![]()
8 years ago
|
* the current interface language is assumed.
|
||
|
*
|
||
|
* @return array returns a multidimensional array with translated week days and months
|
||
![]()
16 years ago
|
*/
|
||
![]()
8 years ago
|
function &_api_get_day_month_names($language = null)
|
||
|
{
|
||
![]()
8 years ago
|
static $date_parts = [];
|
||
![]()
11 years ago
|
if (empty($language)) {
|
||
|
$language = api_get_interface_language();
|
||
|
}
|
||
|
if (!isset($date_parts[$language])) {
|
||
![]()
8 years ago
|
$week_day = [
|
||
![]()
10 years ago
|
'Sunday',
|
||
|
'Monday',
|
||
|
'Tuesday',
|
||
|
'Wednesday',
|
||
|
'Thursday',
|
||
|
'Friday',
|
||
|
'Saturday',
|
||
![]()
8 years ago
|
];
|
||
|
$month = [
|
||
![]()
10 years ago
|
'January',
|
||
|
'February',
|
||
|
'March',
|
||
|
'April',
|
||
|
'May',
|
||
|
'June',
|
||
|
'July',
|
||
|
'August',
|
||
|
'September',
|
||
|
'October',
|
||
|
'November',
|
||
|
'December',
|
||
![]()
8 years ago
|
];
|
||
![]()
11 years ago
|
for ($i = 0; $i < 7; $i++) {
|
||
![]()
8 years ago
|
$date_parts[$language]['days_short'][] = get_lang(
|
||
|
$week_day[$i].'Short',
|
||
|
'',
|
||
|
$language
|
||
|
);
|
||
|
$date_parts[$language]['days_long'][] = get_lang(
|
||
|
$week_day[$i].'Long',
|
||
|
'',
|
||
|
$language
|
||
|
);
|
||
![]()
11 years ago
|
}
|
||
|
for ($i = 0; $i < 12; $i++) {
|
||
![]()
8 years ago
|
$date_parts[$language]['months_short'][] = get_lang(
|
||
|
$month[$i].'Short',
|
||
|
'',
|
||
|
$language
|
||
|
);
|
||
|
$date_parts[$language]['months_long'][] = get_lang(
|
||
|
$month[$i].'Long',
|
||
|
'',
|
||
|
$language
|
||
|
);
|
||
![]()
11 years ago
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return $date_parts[$language];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns returns person name convention for a given language.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $language the input language
|
||
|
* @param string $type The type of the requested convention.
|
||
|
* It may be 'format' for name order convention or 'sort_by' for name sorting convention.
|
||
|
*
|
||
![]()
11 years ago
|
* @return mixed Depending of the requested type,
|
||
![]()
8 years ago
|
* the returned result may be string or boolean; null is returned on error;
|
||
![]()
11 years ago
|
*/
|
||
|
function _api_get_person_name_convention($language, $type)
|
||
|
{
|
||
|
static $conventions;
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
$language = api_get_language_from_iso($language);
|
||
![]()
7 years ago
|
$languageName = 'english';
|
||
|
if (!empty($language)) {
|
||
|
$languageName = $language->getEnglishName();
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
if (!isset($conventions)) {
|
||
![]()
9 years ago
|
$file = __DIR__.'/internationalization_database/name_order_conventions.php';
|
||
![]()
11 years ago
|
if (file_exists($file)) {
|
||
![]()
8 years ago
|
$conventions = include $file;
|
||
![]()
11 years ago
|
} else {
|
||
![]()
8 years ago
|
$conventions = [
|
||
|
'english' => [
|
||
![]()
11 years ago
|
'format' => 'title first_name last_name',
|
||
![]()
8 years ago
|
'sort_by' => 'first_name',
|
||
|
],
|
||
![]()
8 years ago
|
];
|
||
![]()
11 years ago
|
}
|
||
|
// Overwrite classic conventions
|
||
|
$customConventions = api_get_configuration_value('name_order_conventions');
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
if (!empty($customConventions)) {
|
||
|
foreach ($customConventions as $key => $data) {
|
||
|
$conventions[$key] = $data;
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
$search1 = ['FIRST_NAME', 'LAST_NAME', 'TITLE'];
|
||
|
$replacement1 = ['%F', '%L', '%T'];
|
||
|
$search2 = ['first_name', 'last_name', 'title'];
|
||
|
$replacement2 = ['%f', '%l', '%t'];
|
||
![]()
11 years ago
|
foreach (array_keys($conventions) as $key) {
|
||
|
$conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
|
||
![]()
8 years ago
|
$conventions[$key]['format'] = _api_validate_person_name_format(
|
||
|
_api_clean_person_name(
|
||
|
str_replace(
|
||
|
'%',
|
||
|
' %',
|
||
|
str_ireplace(
|
||
|
$search2,
|
||
|
$replacement2,
|
||
|
$conventions[$key]['format']
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
);
|
||
![]()
11 years ago
|
$conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
|
||
|
}
|
||
|
}
|
||
|
switch ($type) {
|
||
|
case 'format':
|
||
![]()
7 years ago
|
return is_string($conventions[$languageName]['format']) ? $conventions[$languageName]['format'] : '%t %f %l';
|
||
![]()
11 years ago
|
case 'sort_by':
|
||
![]()
7 years ago
|
return is_bool($conventions[$languageName]['sort_by']) ? $conventions[$languageName]['sort_by'] : true;
|
||
![]()
11 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Replaces non-valid formats for person names with the default (English) format.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $format the input format to be verified
|
||
|
*
|
||
|
* @return bool returns the same format if is is valid, otherwise returns a valid English format
|
||
![]()
11 years ago
|
*/
|
||
![]()
9 years ago
|
function _api_validate_person_name_format($format)
|
||
|
{
|
||
![]()
11 years ago
|
if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
|
||
|
return '%t %f %l';
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return $format;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Removes leading, trailing and duplicate whitespace and/or commas in a full person name.
|
||
![]()
8 years ago
|
* Cleaning is needed for the cases when not all parts of the name are available
|
||
|
* or when the name is constructed using a "dirty" pattern.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $person_name the input person name
|
||
|
*
|
||
|
* @return string returns cleaned person name
|
||
![]()
11 years ago
|
*/
|
||
![]()
9 years ago
|
function _api_clean_person_name($person_name)
|
||
|
{
|
||
![]()
8 years ago
|
return preg_replace(['/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'], [' ', ', ', ',', '', ''], $person_name);
|
||
![]()
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Appendix to "Multibyte string conversion functions".
|
||
![]()
11 years ago
|
*/
|
||
|
|
||
|
/**
|
||
|
* This is a php-implementation of a function that is similar to mb_convert_encoding() from mbstring extension.
|
||
|
* The function converts a given string from one to another character encoding.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string the string being converted
|
||
|
* @param string $to_encoding the encoding that $string is being converted to
|
||
|
* @param string $from_encoding the encoding that $string is being converted from
|
||
|
*
|
||
|
* @return string returns the converted string
|
||
![]()
11 years ago
|
*/
|
||
|
function _api_convert_encoding(&$string, $to_encoding, $from_encoding)
|
||
|
{
|
||
|
return mb_convert_encoding($string, $to_encoding, $from_encoding);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This function determines the name of corresponding to a given encoding conversion table.
|
||
|
* It is able to deal with some aliases of the encoding.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $encoding the given encoding identificator, for example 'WINDOWS-1252'
|
||
|
*
|
||
|
* @return string returns the name of the corresponding conversion table, for the same example - 'CP1252'
|
||
![]()
11 years ago
|
*/
|
||
![]()
9 years ago
|
function _api_get_character_map_name($encoding)
|
||
|
{
|
||
![]()
11 years ago
|
static $character_map_selector;
|
||
|
if (!isset($character_map_selector)) {
|
||
![]()
9 years ago
|
$file = __DIR__.'/internationalization_database/conversion/character_map_selector.php';
|
||
![]()
11 years ago
|
if (file_exists($file)) {
|
||
![]()
8 years ago
|
$character_map_selector = include $file;
|
||
![]()
11 years ago
|
} else {
|
||
![]()
8 years ago
|
$character_map_selector = [];
|
||
![]()
11 years ago
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
|
||
|
}
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Appendix to "String comparison".
|
||
![]()
11 years ago
|
*/
|
||
|
|
||
|
/**
|
||
![]()
8 years ago
|
* A reverse function from php-core function strnatcmp(),
|
||
|
* performs string comparison in reverse natural (alpha-numerical) order.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $string1 the first string
|
||
|
* @param string $string2 the second string
|
||
|
*
|
||
|
* @return int returns 0 if $string1 = $string2; >0 if $string1 < $string2; <0 if $string1 > $string2
|
||
![]()
11 years ago
|
*/
|
||
![]()
10 years ago
|
function _api_strnatrcmp($string1, $string2)
|
||
|
{
|
||
![]()
11 years ago
|
return strnatcmp($string2, $string1);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets/Gets internal character encoding of the common string functions within the PHP mbstring extension.
|
||
![]()
8 years ago
|
*
|
||
![]()
8 years ago
|
* @param string $encoding (optional) When this parameter is given, the function sets the internal encoding
|
||
![]()
8 years ago
|
*
|
||
|
* @return string When $encoding parameter is not given, the function returns the internal encoding.
|
||
![]()
8 years ago
|
* Note: This function is used in the global initialization script for setting the
|
||
|
* internal encoding to the platform's character set.
|
||
![]()
8 years ago
|
*
|
||
|
* @see http://php.net/manual/en/function.mb-internal-encoding
|
||
![]()
11 years ago
|
*/
|
||
![]()
11 years ago
|
function _api_mb_internal_encoding($encoding = 'UTF-8')
|
||
![]()
11 years ago
|
{
|
||
|
return mb_internal_encoding($encoding);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks whether the specified encoding is supported by the PHP mbstring extension.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $encoding the specified encoding
|
||
|
*
|
||
|
* @return bool returns TRUE when the specified encoding is supported, FALSE othewise
|
||
![]()
11 years ago
|
*/
|
||
![]()
9 years ago
|
function _api_mb_supports($encoding)
|
||
|
{
|
||
![]()
8 years ago
|
static $supported = [];
|
||
![]()
11 years ago
|
if (!isset($supported[$encoding])) {
|
||
|
if (MBSTRING_INSTALLED) {
|
||
|
$supported[$encoding] = api_equal_encodings($encoding, mb_list_encodings(), true);
|
||
|
} else {
|
||
|
$supported[$encoding] = false;
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return $supported[$encoding];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Checks whether the specified encoding is supported by the PHP iconv extension.
|
||
![]()
8 years ago
|
*
|
||
|
* @param string $encoding the specified encoding
|
||
|
*
|
||
|
* @return bool returns TRUE when the specified encoding is supported, FALSE othewise
|
||
![]()
11 years ago
|
*/
|
||
![]()
9 years ago
|
function _api_iconv_supports($encoding)
|
||
|
{
|
||
![]()
8 years ago
|
static $supported = [];
|
||
![]()
11 years ago
|
if (!isset($supported[$encoding])) {
|
||
|
if (ICONV_INSTALLED) {
|
||
|
$enc = api_refine_encoding_id($encoding);
|
||
|
if ($enc != 'HTML-ENTITIES') {
|
||
|
$test_string = '';
|
||
|
for ($i = 32; $i < 128; $i++) {
|
||
|
$test_string .= chr($i);
|
||
|
}
|
||
|
$supported[$encoding] = (@iconv_strlen($test_string, $enc)) ? true : false;
|
||
|
} else {
|
||
|
$supported[$encoding] = false;
|
||
|
}
|
||
|
} else {
|
||
|
$supported[$encoding] = false;
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return $supported[$encoding];
|
||
|
}
|
||
|
|
||
|
// This function checks whether the function _api_convert_encoding() (the php-
|
||
|
// implementation) is able to convert from/to a given encoding.
|
||
![]()
9 years ago
|
function _api_convert_encoding_supports($encoding)
|
||
|
{
|
||
![]()
8 years ago
|
static $supports = [];
|
||
![]()
11 years ago
|
if (!isset($supports[$encoding])) {
|
||
|
$supports[$encoding] = _api_get_character_map_name(api_refine_encoding_id($encoding)) != '';
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
11 years ago
|
return $supports[$encoding];
|
||
|
}
|
||
![]()
9 years ago
|
|
||
|
/**
|
||
![]()
8 years ago
|
* Given a date object, return a human or ISO format, with or without h:m:s.
|
||
|
*
|
||
|
* @param object $date The Date object
|
||
|
* @param bool $showTime Whether to show the time and date (true) or only the date (false)
|
||
|
* @param bool $humanForm Whether to show day-month-year (true) or year-month-day (false)
|
||
|
*
|
||
![]()
9 years ago
|
* @return string Formatted date
|
||
|
*/
|
||
![]()
9 years ago
|
function api_get_human_date_time($date, $showTime = true, $humanForm = false)
|
||
|
{
|
||
![]()
9 years ago
|
if ($showTime) {
|
||
![]()
9 years ago
|
if ($humanForm) {
|
||
![]()
8 years ago
|
return $date->format('j M Y H:i:s');
|
||
![]()
9 years ago
|
} else {
|
||
![]()
8 years ago
|
return $date->format('Y-m-d H:i:s');
|
||
![]()
9 years ago
|
}
|
||
|
} else {
|
||
![]()
9 years ago
|
if ($humanForm) {
|
||
![]()
8 years ago
|
return $date->format('j M Y');
|
||
![]()
9 years ago
|
} else {
|
||
![]()
8 years ago
|
return $date->format('Y-m-d');
|
||
![]()
9 years ago
|
}
|
||
|
}
|
||
|
}
|