5

Hey, this is my first post on stackoverflow.

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.

Thanks,

Jason Tolhurst

Jason Tolhurst
  • 53
  • 1
  • 1
  • 3
  • possible duplicate of [Need to convert é to e in PHP](http://stackoverflow.com/questions/4125658/need-to-convert-e-to-e-in-php) – Mark Baker Nov 08 '10 at 17:12

3 Answers3

8
$string = iconv('utf-8','ASCII//IGNORE//TRANSLIT',$string);
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • yeah that drops the e off the end. – Jason Tolhurst Nov 08 '10 at 17:08
  • It doesn't here.....Can you reach the command line, and see if both charsets are supported with `iconv -l`? And what does `mb_detect_encoding($string);` say about the probably character set of the string (it might not be utf-8)? – Wrikken Nov 08 '10 at 17:09
  • I can get there how do I check it? – Jason Tolhurst Nov 08 '10 at 17:11
  • Oops typed it wrong the first time. Yes both ASCII and UTF-8 are supported. We tested this code and str_replace on another page on the same server and they functions as expected. So something else is wrong here with our script :P Thanks for the help though. – Jason Tolhurst Nov 08 '10 at 17:19
  • I'd still say the input-charset isn't utf-8 :) What happens if you try one of the usual other input suspects (iso-8859-15, cp1252. etc.)? – Wrikken Nov 08 '10 at 17:21
  • From the [docs](https://www.php.net/manual/en/function.iconv.php): "If you append the string *//IGNORE*, characters that cannot be represented in the target charset are silently discarded." - I had to remove `//IGNORE` to make it [work](https://rextester.com/GNJG65315) – Paul Spiegel May 22 '19 at 11:06
5

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);
}
TRiG
  • 10,148
  • 7
  • 57
  • 107
  • 1
    Yay for `€`, `±`, `¼`,`&` and [loads of others](http://www.w3.org/TR/WD-html40-970708/sgml/entities.html), IMHO you're introducing new problems, although the original problem is solved indeed. – Wrikken Nov 08 '10 at 17:15
  • You're quite right, though that may or may not be a problem, depending on the use-case. – TRiG Nov 08 '10 at 17:16
  • `$string = preg_replace(array('/ß/', '/&(..)lig;/', '/&([aeioucyAEIOUCY])(uml|acute|ring|cedil|circ|tilde|grave|slash);/', '/&(.)[^;]*;/'), array('ss', "$1", "$1", ""), $string);` – Aistina Nov 08 '10 at 17:30
2

See the php manual page for strtr()

The examples on this page deal with exactly your situation.

Hope that helps.

Josh
  • 10,961
  • 11
  • 65
  • 108
Spudley
  • 166,037
  • 39
  • 233
  • 307