2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Safe;
|
|
|
|
|
|
|
|
use Safe\Exceptions\IconvException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve internal configuration variables of iconv extension.
|
|
|
|
*
|
|
|
|
* @param string $type The value of the optional type can be:
|
|
|
|
*
|
|
|
|
* all
|
|
|
|
* input_encoding
|
|
|
|
* output_encoding
|
|
|
|
* internal_encoding
|
|
|
|
*
|
|
|
|
* @return mixed Returns the current value of the internal configuration variable if
|
|
|
|
* successful.
|
|
|
|
*
|
|
|
|
* If type is omitted or set to "all",
|
|
|
|
* iconv_get_encoding returns an array that
|
|
|
|
* stores all these variables.
|
|
|
|
* @throws IconvException
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function iconv_get_encoding(string $type = "all")
|
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
$result = \iconv_get_encoding($type);
|
|
|
|
if ($result === false) {
|
|
|
|
throw IconvException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the value of the internal configuration variable specified by
|
2022-07-12 19:26:21 +00:00
|
|
|
* type to encoding.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
|
|
|
* @param string $type The value of type can be any one of these:
|
|
|
|
*
|
|
|
|
* input_encoding
|
|
|
|
* output_encoding
|
|
|
|
* internal_encoding
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param string $encoding The character set.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws IconvException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function iconv_set_encoding(string $type, string $encoding): void
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \iconv_set_encoding($type, $encoding);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw IconvException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-07-12 19:26:21 +00:00
|
|
|
* Converts string from from_encoding
|
|
|
|
* to to_encoding.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param string $from_encoding The current encoding used to interpret string.
|
|
|
|
* @param string $to_encoding The desired encoding of the result.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* If the string //TRANSLIT is appended to
|
|
|
|
* to_encoding, then transliteration is activated. This
|
2021-02-26 16:16:17 +00:00
|
|
|
* means that when a character can't be represented in the target charset,
|
2022-07-12 19:26:21 +00:00
|
|
|
* it may be approximated through one or several similarly looking
|
|
|
|
* characters. If the string //IGNORE is appended,
|
2021-02-26 16:16:17 +00:00
|
|
|
* characters that cannot be represented in the target charset are silently
|
|
|
|
* discarded. Otherwise, E_NOTICE is generated and the function
|
|
|
|
* will return FALSE.
|
|
|
|
*
|
|
|
|
* If and how //TRANSLIT works exactly depends on the
|
|
|
|
* system's iconv() implementation (cf. ICONV_IMPL).
|
|
|
|
* Some implementations are known to ignore //TRANSLIT,
|
|
|
|
* so the conversion is likely to fail for characters which are illegal for
|
2022-07-12 19:26:21 +00:00
|
|
|
* the to_encoding.
|
|
|
|
* @param string $string The string to be converted.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @return string Returns the converted string.
|
|
|
|
* @throws IconvException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function iconv(string $from_encoding, string $to_encoding, string $string): string
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \iconv($from_encoding, $to_encoding, $string);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw IconvException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|