0

I'm having a really hard time trying to replace a certain character with it's entity equivalent. I tried to str_replace the character "ā" but it just wont work! I can't see why. The code I use:

$content = str_replace('ā', 'ā',$content);

If anyone has some tips or maybe even an explanation of why it doesn't work, please let me know!

UPDATE:

While above code doesn't replace the character with the entity (don't understand why) that is not the main problem. The character shows up fine, but when I read this page using file_get_contents and after that insert the read HTML into a database the character gets mangled. It's already mangled in the database in which it was inserted.

All headers as suggested below are UTF-8, database tables have UTF-8 encoding, files are UTF-8...

As a quick fix I wanted to convert the char to it's entity before insert into that DB.

tvgemert
  • 1,436
  • 3
  • 25
  • 50

2 Answers2

1

Try this: header('Content-Type: text/html; charset=utf-8'); This will make your page display all the legal characters by UTF8, place this code on your page right after <?php

UPDATE: Try at every connection to DB:

    $connect = YOUR_MYSQL_CONNECTION();

    mysql_query( "SET NAMES 'utf8';" , $connect );

    mysql_query( "SET CHARACTER SET 'utf8';" , $connect );
Michael Sazonov
  • 1,531
  • 11
  • 21
1

Is the larger string which contains ā easily accessible? If so, the htmlentities function should do the job. It should convert all characters with an HTML equivalent to that equivalent. However, it'll also convert the likes of < into &lt;.

chooban
  • 9,018
  • 2
  • 20
  • 36
  • Well... Thanks, but I'm quite well aware of what htmlentities does, that's not the point. I'm totally puzzled why it doesn't work in this specific situation. – tvgemert May 14 '12 at 07:42
  • Sorry, got myself mixed up. There are a few other questions on SO that suggest that str_replace isn't always multibyte happy. [This question](http://stackoverflow.com/questions/1451144/php-multi-byte-str-replace) seems to be describing something similar. When you say the character is mangled in the database what do you mean? – chooban May 17 '12 at 06:59