Getting "instead of the apostrophe" (") in PHP

I tried converting text to / from utf8, which didn't seem to help.

I get:

"It’s Getting the Best of Me" 

It should be:

 "It's Getting the Best of Me" 

I get this data from this URL.

+53
php utf-8 character-encoding mojibake
Feb 18 '10 at 20:33
source share
14 answers

To convert to HTML objects:

 <?php echo mb_convert_encoding( file_get_contents('http://www.tvrage.com/quickinfo.php?show=Surviver&ep=20x02&exact=0'), "HTML-ENTITIES", "UTF-8" ); ?> 

See the docs for mb_convert_encoding for more on coding.

+83
Feb 18 2018-10-18
source share

Make sure your html header indicates utf8

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

This usually does the trick for me (obviously if the contents of IS are utf8).

You do not need to convert html objects if you specify the content type.

+27
Mar 24 '10 at 6:21
source share

Your content is OK; The problem is with the headers sent by the server:

 Connection:Keep-Alive Content-Length:502 Content-Type:text/html Date:Thu, 18 Feb 2010 20:45:32 GMT Keep-Alive:timeout=1, max=25 Server:Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.7 with Suhosin-Patch X-Powered-By:PHP/5.2.4-2ubuntu5.7 

Content-Type must be set to Content-type: text/plain; charset=utf-8 Content-type: text/plain; charset=utf-8 because this page is not HTML and uses utf-8 encoding. Chromium on Mac guesses ISO-8859-1 and displays the characters you describe.

If you do not control the site, specify the encoding as UTF-8 for any function that you use to extract the content. I am not familiar with PHP to know exactly how.

+12
Feb 18 2018-10-18
source share

I know that the question was answered, but setting the meta tag did not help in my case, and the selected answer was not clear enough, so I wanted to give a simpler answer.

So, to make it simple, save the string in a variable and process it like this

 $TVrageGiberish = "It’s Getting the Best of Me"; $notGiberish = mb_convert_encoding($TVrageGiberish, "HTML-ENTITIES", 'UTF-8'); echo $notGiberish; 

Which should return what you need It's Getting the Best of Me

If you parse something, you can perform the conversion by assigning values ​​to a variable like this, where $TVrage is an array with all the values, the XML in this example is from a feed with a "Title" tag, which can contain special characters, such as ‘ or ’ .

 $cleanedTitle = mb_convert_encoding($TVrage->title, "HTML-ENTITIES", 'UTF-8'); 
+8
Sep 11 '13 at 14:41
source share

If you are here because you have problems with unwanted characters on your WordPress site, try the following:

  • Open wp-config.php

  • Comment define('DB_CHARSET', 'utf8') and define('DB_COLLATE', '')

     /** MySQL hostname */ define('DB_HOST', 'localhost'); /** Database Charset to use in creating database tables. */ //define('DB_CHARSET', 'utf8'); /** The Database Collate type. Don't change this if in doubt. */ //define('DB_COLLATE', ''); 
+5
Jun 21 '12 at 18:08
source share

It looks like you are using standard string functions for UTF8 () characters that are not in ISO 8859-1 . Make sure you are using Unicode compatible PHP Settings and Features. See also multibyte functions.

+3
Feb 18 '10 at 20:41
source share

if everything does not seem to work, this might be your best solution.

 <?php $content="It’s Getting the Best of Me"; $content = str_replace("’", "&#39;", $content); echo $content; ?> 

== or ==

 <?php $content="It’s Getting the Best of Me"; $content = str_replace("’", "'", $content); echo $content; ?> 
+2
Aug 18 '16 at 15:00
source share

I looked at the link and it seems to me UTF-8. that is, in Firefox, if you select View, the character encoding, UTF-8, it will appear correctly.

So, you just need to figure out how to get your PHP code to treat this like UTF-8. Good luck

+1
Feb 18 '10 at 20:40
source share

try the following:

 html_entity_decode(mb_convert_encoding(stripslashes($text), "HTML-ENTITIES", 'UTF-8')) 
+1
Oct 17 '13 at 15:22
source share

We had success in another direction using this:

 mb_convert_encoding($text, "HTML-ENTITIES", "ISO-8859-1"); 
+1
Mar 20 '14 at 22:09
source share

Just try this

If $text contains strange characters, do the following:

 $mytext = mb_convert_encoding($text, "HTML-ENTITIES", 'UTF-8'); 

and you're done.

+1
Feb 04 '16 at 5:47
source share

For fopen and file_put_contents this will work:

 str_replace("&rsquo;", "'", htmlspecialchars_decode(mb_convert_encoding($string_to_be_fixed, "HTML-ENTITIES", "UTF-8"))); 
+1
Apr 26 '17 at 17:46 on
source share

use this

 <meta http-equiv="Content-Type" content="text/html; charset=utf8_unicode_ci" /> 

instead of this

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
0
Feb 15 '13 at 10:33
source share

You should check the source of the encoding, and then try to convert the code to the correct type.

In my case, I read CSV files and then import into the database. Some files display well, some do not. I check the encoding and see that the ASCII encoded file displays well, the other UTF-8 file is damaged. Therefore, I use the following code to convert the encoding:

 if(mb_detect_encoding($content) == 'UTF-8') { $content = iconv("UTF-8", "ASCII//TRANSLIT", $content); file_put_contents($file_path, $content); } else { $content = mb_convert_encoding($content, 'UTF-8', 'UTF-8'); file_put_contents($file_path, $content); } 

After conversion, I put the contents into a file, then I process the import into the database, now it is displayed well in the interface.

0
Aug 26 '19 at 2:55
source share



All Articles