Getting ’ Instead of an Apostrophe(') in PHP

Getting ’ instead of an apostrophe(') in PHP

To convert to HTML entities:

<?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 docs for mb_convert_encoding for more encoding options.

’s comes up instead of apostrophe ' in PHP

try

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

An apostrophe is rendering as â€tm. What PHP function will display it as ' ? something_Decode?

You could try to use the following function:

function htmlallentities($str){
$res = '';
$strlen = strlen($str);
for($i=0; $i<$strlen; $i++){
$byte = ord($str[$i]);
if($byte < 128) // 1-byte char
$res .= $str[$i];
elseif($byte < 192); // invalid utf8
elseif($byte < 224) // 2-byte char
$res .= '&#'.((63&$byte)*64 + (63&ord($str[++$i]))).';';
elseif($byte < 240) // 3-byte char
$res .= '&#'.((15&$byte)*4096 + (63&ord($str[++$i]))*64 + (63&ord($str[++$i]))).';';
elseif($byte < 248) // 4-byte char
$res .= '&#'.((15&$byte)*262144 + (63&ord($str[++$i]))*4096 + (63&ord($str[++$i]))*64 + (63&ord($str[++$i]))).';';
}
return $res;
}

call:

$str = htmlallentities($str);

this will change utf-8-chars into htmlentities, so you can display them in different encodings.

’ in PHP is converting to ’ when using mb_convert_encoding in Outlook subject

Whatever you used to type the subject did not use a simple apostrophe ' which has a common representation across virtually all single-byte encodings and UTF8, instead it used a "fancy" right single quote , which is represented differently between single-byte encodings and UTF-8.

mb_convert_encoding() is converting to an HTML entity because you are literally telling it to, and email headers are not HTML so it's going to display as the literal string . The only character set other than UTF-8 that has "smart quotes" is Microsoft's cp1252, and that is still the wrong answer for email headers.

The simplest answer is: Don't do that. Use a normal apostrophe. Everyone hates dealing with "smart" quotes.

The more complex answer is that email headers MUST be 7-bit safe "ASCII" text, and anything else requires additional handwaving. Ideally you should be using a proper email library that handles this, and the dozens of other annoyances that will malform your emails and impact deliverability.

If you're dead-set on eroding your sanity and using mail() directly, then you're going to want to properly encode your subject line and use an explicitly-defined character set, which you should be doing anyways. Eg:

$subject = 'Please provide an updated copy of your company’s certification';

var_dump(
sprintf('=?UTF-8?Q?%s?=', quoted_printable_encode($subject))
);

Output:

string(82) "=?UTF-8?Q?Please provide an updated copy of your company=E2=80=99s certification?="

’ showing on page instead of '

Ensure the browser and editor are using UTF-8 encoding instead of ISO-8859-1/Windows-1252.

Or use .

apostrophe is not stored in Mysql database while scraping data from website

Change your database collation to utf8_general_ci

OR change the column type to BLOB. so your collation will be empty.

I think that will work.

Why do symbols like apostrophes and hyphens get replaced with black diamonds on my website?

It's an encoding problem. You have to set the correct encoding in the HTML head via meta tag:

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

Replace "ISO-8859-1" with whatever your encoding is (e.g. 'UTF-8'). You must find out what encoding your HTML files are. If you're on an Unix system, just type file file.html and it should show you the encoding. If this is not possible, you should be able to find out somewhere what encoding your editor produces.

Apostrophe replaced by ’ in mySQL table

You have to make a query before storing data:

SET NAMES utf8.

Apparently it's mysql_set_charset('utf8',$conn) in 5.2+



Related Topics



Leave a reply



Submit