Voting

The Note You're Voting On

desmatic at gmail dot com
12 years ago
quick and dirty loop through multibyte string
<?php
function get_character_classes($string, $encoding = "UTF-8") {
$current_encoding = mb_internal_encoding();
mb_internal_encoding($encoding);
$has = array();
$stringlength = mb_strlen($string, $encoding);
for (
$i=0; $i < $stringlength; $i++) {
$c = mb_substr($string, $i, 1);
if ((
$c >= "0") && ($c <= "9")) {
$has['numeric'] = "numeric";
} else if ((
$c >= "a") && ($c <= "z")) {
$has['alpha'] = "alpha";
$has['alphalower'] = 'alphalower';
} else if ((
$c >= "A") && ($c <= "Z")) {
$has['alpha'] = "alpha";
$has['alphaupper'] = "alphaupper";
} else if ((
$c == "$") || ($c == "£")) {
$has['currency'] = "currency";
} else if ((
$c == ".") && ($has['decimal'])) {
$has['decimals'] = "decimals";
} else if (
$c == ".") {
$has['decimal'] = "decimal";
} else if (
$c == ",") {
$has['comma'] = "comma";
} else if (
$c == "-") {
$has['dash'] = "dash";
} else if (
$c == " ") {
$has['space'] = "space";
} else if (
$c == "/") {
$has['slash'] = "slash";
} else if (
$c == ":") {
$has['colon'] = "colon";
} else if ((
$c >= " ") && ($c <= "~")) {
$has['ascii'] = "ascii";
} else {
$has['binary'] = "binary";
}
}
mb_internal_encoding($current_encoding);

return
$has;
}

$string = "1234asdfA£^_{}|}~žščř";
echo
print_r(get_character_classes($string), true);
?>

Array
(
[numeric] => numeric
[alpha] => alpha
[alphalower] => alphalower
[alphaupper] => alphaupper
[currency] => currency
[ascii] => ascii
[binary] => binary
)

<< Back to user notes page

To Top