Converting ร† to "Ae" in PHP using Str_replace?

For reasons justified by business logic, I need to convert the character "ร†" to "Ae" in a string. However, despite the fact that mb_detect_encoding () tells me that the string is UTF-8, I cannot figure out how to do this. (And for other business logic reasons, it would be a problem for the htmlentities () string before replacing it, as other Google searches suggested.)

What I tried first was using the test string "ther":

return str_replace("ร†", 'Ae', $string); 

Unfortunately, this does not actually find ร† in the text, returning "ร†ther".

 return str_replace(chr(195), 'Ae', $string); 

This finds ร† and replaces it, but after that adds an unknown character, changing it to unusable โ€œAe therโ€. So I tried this:

 $ae_character = mb_convert_encoding('&#' . intval(195) . ';', 'UTF-8', 'HTML-ENTITIES'); return str_replace($ae_character, 'Ae', $string); 

What else could not find the in character in the string. I know this is a UTF-8 problem, but I'm honestly deadlocked on how to search and replace this without adding an extra character after that. Any ideas?

+4
source share
2 answers
 <?php $x = 'ร†mystr'; print str_replace('ร†', 'AE', $x); // prints: AEmystr ?> 

This code works very well, I believe that what you are missing is a change in the encoding of your file. Your .php file must be encoded in UTF-8 or UNICODE. This can be done in some (text) editors or IDEs, for example Eclipse, EditPlus, Notepad ++, etc. Even Notepad on windows 7.

When saving, the "Save / Save As" dialog box opens and usually next to the "Save" button there is a drop-down menu / "Encoding" that allows you to choose between ANSI and UTF-8 (and others). A.

On * nix I think most editors have this, just not sure about the places. If after you do this and make money, edit / save using an editor that just executes ANSI, it will overwrite it with an unknown char, etc.

As for why the code below does not work.

 return str_replace(chr(195), 'Ae', $string); 

This is because unicode char usually contains 2 characters. So what you have above is just the beginning of a unicode char. try the following:

 print str_replace(chr(195).chr(134), 'AE', $x); 

This should also replace it, and may even be preferable, since you (probably do not) should not change the encoding of the file.

+6
source

Source: https://habr.com/ru/post/1332793/


All Articles