RhymeGuy
RhymeGuy

Reputation: 2112

PHP / Replace non latin characters with its counterparts

WORD

AŠA

PHP

<?php

foreach (glob("*.jpg") as $filename) {
    $search = array("Š");
    $replace = array("S");

    $newname = str_replace($search, $replace, $filename);
    echo $filename.'<br>'.$newname;

    //($filename, realpath(dirname(__FILE__)).'/'.$newname);
}

PROBLEM It does not replace character "Š" in word "AŠA"

Upvotes: 2

Views: 7721

Answers (2)

Hubyx Reds
Hubyx Reds

Reputation: 133

The previous method does not work having latin characters such as ü or áéíóú it returns 'a'e'i'o'u instead aeiou.

What about transliterator_transliterate. Works for PHP >= 5.4

$str = 'AŠAáéíóú';    
transliterator_transliterate('Any-Latin; Latin-ASCII;', $str); //ASAaeiou

Reference PHP NET: transliterator_transliterate

Upvotes: 10

Karl Laurentius Roos
Karl Laurentius Roos

Reputation: 4399

Take a look at iconv() which allows you to convert a string to a specified encoding.

Example for your case:

$str = 'AŠA';
$str = iconv('UTF-8', 'ASCII//TRANSLIT', $str); // ASA

Upvotes: 11

Related Questions