-1

Let's say I have following string: $string = "Ne, želim ići kući."

Then I split it with preg_split("/[\s,.]+/",$string) and get an array with every word in the sentence: Array ( [0] => [1] => e [2] => želim [3] => ići [4] => kući [5] => )

How can I now (for every array item) replace all the special characters into standard ascii ones?

Is there an easier method to replace characters like ć and others than the following?

foreach ($array as $element) {
    echo str_replace("ć","c",$element);
    ...
}

Or is there maybe a function which can handle the whole array by itself without any foreach-loops; something like convert_to_ascii($array)

Edit: Because I want to omit the for loop, I think this question is not a duplicate as flagged. My question is regarding a function which can handle and return whole arrays by itself.

  • 1
    look at [this](http://stackoverflow.com/a/26514376/2162347) answer – Dragos Nov 30 '16 at 17:50
  • 5
    Possible duplicate of [Replacing accented characters php](http://stackoverflow.com/questions/3371697/replacing-accented-characters-php) – Dragos Nov 30 '16 at 17:50
  • [`echo iconv('UTF-8', 'ASCII//TRANSLIT', $string);`](http://php.net/manual/en/function.iconv.php) – Sammitch Nov 30 '16 at 17:50
  • @Dragos that answer still uses a for loop, I would like to omit that if possible; and because of not wanting such a loop this question is *not* a duplicate – technical_difficulty Nov 30 '16 at 17:53
  • The question IS a duplicate, you do have there the solution to how to replace the accented characters. How you then apply that is a matter of using array functions like [array_walk()](http://php.net/manual/en/function.array-walk.php) – Dragos Nov 30 '16 at 17:57
  • What's wrong with using a loop for this? Any function you find will almost sure contain a loop; if you just want the syntactic sugar of a clean function call, write a function that loops over the array and replaces your characters. – WillardSolutions Nov 30 '16 at 17:58

1 Answers1

0

IMHO a nicer way to get this job done is to install / enable the native PHP module called translit.

For information, you can visit pecl translit.

Once you've enabled it, you can do something fancy like:

echo Transliterator::transliterate('àéô');

and you get aeo.

For more information, you can have a look at here.

Wolverine
  • 1,712
  • 1
  • 15
  • 18
jiGL
  • 175
  • 8