Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace é with e in PHP

Tags:

php

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

like image 538
Jason Tolhurst Avatar asked Nov 08 '10 17:11

Jason Tolhurst


People also ask

How to replace a string with the E modifier in PHP?

If you do spot something please leave a comment and we will endeavour to correct. The PHP function preg_replace () has powerful functionality in its own right, but extra depth can be added with the inclusion of the e modifier. Take the following bit of code, which just picks out the letters of a string and replaces them with the letter X.

How do I replace a string with X in PHP?

Take the following bit of code, which just picks out the letters of a string and replaces them with the letter X. This is simple enough, but using the e modifier allows us to use PHP functions within the replace parameters.

How do you replace a character in a string?

The str_replace () function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How to replace a string in a multidimensional array in PHP?

A faster way to replace the strings in multidimensional array is to json_encode() it, do the str_replace() and then json_decode() it, like this: <?php. function str_replace_json($search, $replace, $subject){.


2 Answers

$string = iconv('utf-8','ASCII//IGNORE//TRANSLIT',$string);
like image 57
Wrikken Avatar answered Oct 29 '22 08:10

Wrikken


You can use htmlentities() to convert é to &eacute; 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);
}
like image 28
TRiG Avatar answered Oct 29 '22 10:10

TRiG