0

I am picking up some text from another system which has had text pasted in from Word, or other systems.

Sometimes the apostrophes are displaying with these characters. I can do a search and replace but am sure this is not the best way, as there may be other characters which are behaving in a similar fashion.

I have look at this question [Getting ’ instead of an apostrophe(') in PHP but everything I have tried still does not work.

My simplified code is this.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8_unicode_ci" />
    <title></title>
</head>
<body>
    <?php

    $s = "It’s Getting the Best of Me";

    echo $s . "<br>";

    $notGiberish = mb_convert_encoding($s, "HTML-ENTITIES", 'UTF-8');

    echo $notGiberish . "<br>";
    echo html_entity_decode(mb_convert_encoding($s, "HTML-ENTITIES", 'UTF-8')) . "<br>";
    ?>
</body>

If I have to I will search and replace.

Thanks

StripyTiger
  • 877
  • 1
  • 14
  • 31
  • 2
    Use [``](https://stackoverflow.com/a/2505733/2191572) because `utf8_unicode_ci` is not valid for HTML decleration, it's only for MySQL. – MonkeyZeus Dec 13 '18 at 14:56
  • Thanks for this. It doesn't sort the problem though. I did reference the question which it has been marked as duplicate, I have tried everything in there, and I can't get it to work. – StripyTiger Dec 13 '18 at 16:08
  • I really don't understand your issue since you have not provided enough information to reproduce it. If you provide a source file and explain how you are retrieving this data then I might be able to suggest something useful. Is this data going into MySQL and then being retrieved by PHP? If so then it's probably being inserted incorrectly into MySQL so you need to fix this before PHP ever gets to the data. – MonkeyZeus Dec 13 '18 at 16:15
  • Thanks - the data is coming out of an API which I have no control over. I tried to make it clear by creating a straight php file as shown above. The ’ above is the same as what is coming out of the API. I am trying to convert that back to an apostrophe for display on a web page. Running the code in the question is not returning an apostrophe, it keeps the strange characters. If I can run a file as above which converts the apostrophe, I will also be able to fix what is coming out of the API. Thank you - sorry if I am not being clear. – StripyTiger Dec 13 '18 at 16:28
  • Well then the API has bad data or incorrect encoding. Can you provide a link to the API if it's public? My best suggestion is `echo str_replace( '’', "'", $response_from_api ); // <- I assume this is a JSON string? This should work` – MonkeyZeus Dec 13 '18 at 16:53
  • Thanks - sorry its not a public api. I will do the str_replace. Thanks for your help. – StripyTiger Dec 13 '18 at 17:16
  • You can also try getting the source encoding by checking out the [header info](https://stackoverflow.com/a/2292077/2191572) of the response. You would use this info in lieu of `UTF-8`. If the header reveals nothing then contact the API vendor and ask them what the heck is going on with their data. – MonkeyZeus Dec 13 '18 at 17:34
  • See https://stackoverflow.com/q/1487369/2191572 for more thoughts. – MonkeyZeus Dec 13 '18 at 17:38

0 Answers0