Jason Tolhurst
Jason Tolhurst

Reputation: 53

How to replace é with e?

I'm trying to replace é with e and other similar special characters. I've tried str_replace() and converting it from UTF-8 to ASCII but nothing seems to work. When I convert it from UTF-8 to anything it just drops the é off. When I use str_replace() it never catches it and the é is still there.

I have a feeling something is wrong internally on our server because my friend tried str_replace() on his server and it worked fine.

Upvotes: 5

Views: 11426

Answers (4)

Mohamed El Mrabet
Mohamed El Mrabet

Reputation: 633

PHP's transliterator_transliterate() function can convert accented characters to their ASCII equivalents.

$string = "Café Déjà vu Résumé naïve façade";
$converted = transliterator_transliterate('Any-Latin; Latin-ASCII', $string);

echo $converted; // Output: "Cafe Deja vu Resume naive facade"

Explanation

  • 'Any-Latin' converts characters to the Latin script.
  • 'Latin-ASCII' removes accents by converting letters like é to e, ç to c, etc.

You must install the php-intl Extension

Upvotes: 0

Spudley
Spudley

Reputation: 168853

See the php manual page for strtr()

The examples on this page deal with exactly your situation.

Hope that helps.

Upvotes: 2

TRiG
TRiG

Reputation: 10643

You can use htmlentities() to convert é to é and then use a regex to pull out the first character after an ampersand.

function RemoveAccents($string) {
    // From http://theserverpages.com/php/manual/en/function.str-replace.php
    $string = htmlentities($string);
    return preg_replace("/&([a-z])[a-z]+;/i", "$1", $string);
}

Upvotes: 5

Wrikken
Wrikken

Reputation: 70540

$string = iconv('utf-8','ASCII//IGNORE//TRANSLIT',$string);

Upvotes: 8

Related Questions