2

Possible Duplicate:
Getting ’ instead of an apostrophe(') in PHP

I am new to the PHP programming language and I need some help. When writing to a file with PHP, when an apostrophe is written to the file, it actually writes ’s. For example, when I try to write:

Hello it's awesome

the program instead writes:

Hello it’ss awesome

I have tried multiple solutions, but still can't get it working. If somebody could help, I would greatly appreciate it.

EDIT: Ok so this is what i have tried:

echo mb_convert_encoding( file_get_contents($filename), "HTML-ENTITIES", "UTF-8" );

The result was: \"Hello\" instead of "Hello"

I have also tried:

$text = fgets($fp); $html = mb_convert_encoding($text, "HTML-ENTITIES", "UTF-8");

But didnt really understand what to do with that code

Also i would like to either convert a variable $stringDatad from ’s characters or convert the whole file from ’s characters. The file name is saved at $filename

Community
  • 1
  • 1
  • Did you try `content="text/html; charset=ISO-8859-1"` in your meta tag? – hjpotter92 Feb 02 '13 at 19:07
  • I just tried echo mb_convert_encoding( file_get_contents('http://www.tvrage.com/quickinfo.php?show=Surviver&ep=20x02&exact=0'), "HTML-ENTITIES", "UTF-8" ); But instead of a " i got a \" –  Feb 02 '13 at 19:07
  • How about you put up all your tries and their results in the question too? – hjpotter92 Feb 02 '13 at 19:12

2 Answers2

5

try

echo stripslashes(mb_convert_encoding( file_get_contents($filename), "HTML-ENTITIES", "UTF-8" ));
  • ok, so how would i save that to a variable and then overwrite the file ($filename) with that new unencoded file? –  Feb 02 '13 at 19:26
  • to assign to a variable just do: $variable_to_write = stripslashes(mb_convert_encoding( file_get_contents($filename), "HTML-ENTITIES", "UTF-8" )); and then use one of the writing functions to write to the file: http://php.net/manual/en/function.file-put-contents.php there are some examples there – Povilas Daukas Feb 02 '13 at 19:32
  • ok so i used file_put_contents for that. So i get this error that says expected parameter 1 to be a string. This means that that second part (because parameter 0 would be first part) has to be a string? what if i want the second part to be $file_get_contects?? –  Feb 02 '13 at 19:44
  • would be good to see the code just to make sure you havent missed anything out – Povilas Daukas Feb 02 '13 at 19:55
  • ok my code is: mb_convert_encoding( file_get_contents($filename), "HTML-ENTITIES", "UTF-8" ); $variable_to_write = stripslashes(mb_convert_encoding( file_get_contents($filename), "HTML-ENTITIES", "UTF-8" )); // file_put_contents($ourFileHandle,$variable_to_write); I need the file to be overwritten, not add the new file to the end. That file_put_contents needs a string not a variable. How would i overwrite the file? –  Feb 02 '13 at 20:04
  • I GOT IT!!!!!!! YAY! I just used a simple replace script and that variable_to_write you gave me!! Thank you soooo much!! voting you UP! –  Feb 02 '13 at 20:17
0

I think mb_convert_encoding takes $to/$from_encoding as parameters: mb_convert_encoding($str, 'UTF-8', 'ISO-8859-1');

Is HTML-ENTITIES a valid value for the first parameter after the string?

nkkollaw
  • 1,947
  • 1
  • 19
  • 29