Schlagwort-Archive: php5

Detect charset with php5 – utf8 or not


/**
* Checks the charset of a text by a given charst
* Works by comparing text with double encoded text.
*
* @param string $text The text string to check for charset.
* IE "test" or "töster" or "100 €" or "Пред" or "留学生"
* @param string $encoding The charset to check with.
* IE "ASCII" or "ISO-8859-1" or "ISO-8859-15" or "UTF-8"
* @param string $middle_encoding The charset to use between the both encoding steps.
* "UTF-8" is default.
* @return boolean true or false
* @author Christof E. Spies
* @copyright ICEF GmbH
* @since 2009-04-20
* @link http://de.php.net/mb_convert_encoding
*/
function check_charset ( $text, $encoding, $middle_encoding = "UTF-8" )
{
return $text === mb_convert_encoding ( mb_convert_encoding ( $text, $encoding, $middle_encoding ), $middle_encoding , $encoding );
}

/**
* Detects the charset of a text from an array of charsets
*
* @param string $text The text string to check for charset. IE "test" or "töster" or "100 €" or "Пред" or "留学生"
* @param array $encoding The charset to check with.
* Default is array(
* "ASCII",
* "ISO-8859-1",
* "ISO-8859-15",
* "UTF-8"
* );
* @retrun string charset or false
* @author Christof E. Spies
* @copyright ICEF GmbH
* @since 2009-04-20
*/
function detect_charset ($text, array $possible_charsets=array("ASCII","ISO-8859-1","ISO-8859-15","UTF-8"))
{
foreach ($possible_charsets as $code)
{
if (check_charset($text, $code))
{
return $code;
}
}
return false;
}