0
function make_ascii($str) {
    $special = array('ľ','š','č','ť','ž','ý','á','í','é','ú','ä','ô','ň','ě');
    $ascii =   array('l','s','c','t','z','y','a','i','e','u','a','o','n','e');
    $str = str_split($str);
    foreach ($str as $k => $c) {
        if(ctype_upper($c)) {
            $u = true;
            $c = strtolower($c);
        } else {
            $u = false;
        }
        if(in_array($c, $special, false)) {
            $c = $ascii[array_search($c, $special)];
        }
        if($u) {
            $c = strtoupper($c);
        }
        $str[$k] = $c;
    }
    return join($str);
}

In this function, even if I feed characters from $special array the in_array() returns false every time, if I would var_dump() on regular text I try to parse, the output will be just bool(false) with no mach, even if I copy paste the character from source to array. Also I'm looking for way to make this character replacement work.

azurinko
  • 261
  • 2
  • 11

2 Answers2

1

As str_splitwill not work for multibyte you have to use mb_to perform multibyte string operation

function make_ascii($str) {
    //return $str;
    $special = array('ľ','š','č','ť','ž','ý','á','í','é','ú','ä','ô','ň','ě');
    $ascii =   array('l','s','c','t','z','y','a','i','e','u','a','o','n','e');
    $str = array_map(function ($i) use ($str) { 
    return mb_substr($str, $i, 1); 
}, range(0, mb_strlen($str) -1));
    foreach ($str as $k => $c) {
        if(ctype_upper($c)) {
            $u = true;
            $c = strtolower($c);
        } else {
            $u = false;
        }
       // print_r($c);
        if(in_array($c, $special)) {
            $c = $ascii[array_search($c, $special)];
        }
        if($u) {
            $c = strtoupper($c);
        }
        $str[$k] = $c;
    }
    return join($str);
}
var_dump(make_ascii('áé'));

DEMO

If issue with uppercase letters you have to change functions to mb_strtoupper and mb_strtolower. Also ctype_upper will not work so change it also

function make_ascii($str) {
    //return $str;
    $special = array('ľ','š','č','ť','ž','ý','á','í','é','ú','ä','ô','ň','ě');
    $ascii =   array('l','s','c','t','z','y','a','i','e','u','a','o','n','e');
    $str = array_map(function ($i) use ($str) { 
    return mb_substr($str, $i, 1); 
}, range(0, mb_strlen($str) -1));

    foreach ($str as $k => $c) {
        if( mb_strtoupper($c, "UTF-8") == $c) {
            $u = true;
            $c = mb_strtolower($c);
        } else {
            $u = false;
        }
       // print_r($c);
        if(in_array($c, $special)) {
            $c = $ascii[array_search($c, $special)];
        }
        if($u) {
            $c = mb_strtoupper($c);
        }
        $str[$k] = $c;
    }
    return join($str);
}
$str = "ľÁľa ýellow";
var_dump(make_ascii($str));

DEMO

B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • sounds good, demo is working, but somehow here on my text it doesn't get past in_array, that always gets false. – azurinko Sep 28 '17 at 10:40
  • Sidenote: (also I realised for my answer too), `strtolower` will also not work. We probalby need [`mb_strtolower`](http://php.net/manual/en/function.mb-strtolower.php) – apokryfos Sep 28 '17 at 10:41
  • check your string https://3v4l.org/Y9ZOh If you have diffrent string give that string – B. Desai Sep 28 '17 at 10:41
0

As I mentioned str_split can't handle multi-byte strings. Don't believe me? Have a look.

At any rate here's a version that can split multibyte strings:

function make_ascii($str) {

    $special = array('ľ','š','č','ť','ž','ý','á','í','é','ú','ä','ô','ň','ě');
    $ascii =   array('l','s','c','t','z','y','a','i','e','u','a','o','n','e');
    $str = preg_split("//u",$str);
    foreach ($str as $k => $c) {
        if(ctype_upper($c)) {
            $u = true;
            $c = mb_strtolower($c);
        } else {
            $u = false;
        }
        if(in_array($c, $special, false)) {
            $c = $ascii[array_search($c, $special)];
        }
        if($u) {
            $c = mb_strtoupper($c);
        }
        $str[$k] = $c;
    }
    return join($str);
}

$str = "ľaľa ýellow";

print_r(make_ascii($str));

Prints:

lala yellow

apokryfos
  • 38,771
  • 9
  • 70
  • 114