Htmlentities not working

Can someone please look at this code and tell me what I’m doing wrong? I have run phpinfo and it is all set to UTF-8, but my htmlentities aren’t converting.

<!DOCTYPE html>

<head>
<meta charset="UTF-8">
</head>

<html>
<body style="margin-left:50px">

<?php

$stringin="“I think Carey … is steering away from the concept that a mystery novel is solely about who did it. For her it is more a case of whether people will get away with their misdeeds…”—Kate Jackson, Cross Examining Crime. New introduction by Curtis Evans. July 2022.";

echo $stringin;

$new = htmlentities($stringin, ENT_QUOTES | ENT_DISALLOWED | ENT_SUBSTITUTE, "UTF-8");

echo "<br><br>new = ";
echo $new;

?>

</body>
</html>

It is actually working. The browser is converting what you see. If you add

header(‘Content-Type: text/plain’);

you will see what is actually there.

<?php
$stringin="“I think Carey … is steering away from the concept that a mystery novel is solely about who did it. For her it is more a case of whether people will get away with their misdeeds…”—Kate Jackson, Cross Examining Crime. New introduction by Curtis Evans. July 2022.";
$new = htmlentities($stringin, ENT_QUOTES | ENT_DISALLOWED | ENT_SUBSTITUTE, "UTF-8");
header('Content-Type: text/plain');
echo $new;

That doesn’t solve my problem. I need the string to appear on the screen with all of the non-standard characters – quotes, ellipses, emdashes, etc. – visible to the user.

What is the real problem you are trying to solve by doing this?

My user enters a string with quotes, ellipses, italics, etc. I need to be able to store that string in MySQL. Later, I will get that string and use it to populate a webpage. When it appears on the webpage, it needs to appear just how the user wrote it, with quotes, ellipses, etc.

Ordinarily, I would write html codes to replace those characters for each instance, but I want it automated so that I don’t have to rewrite every string by hand.

If your putting it in a database, then put it in there. Use PDO with Prepared Statements.

When you output the data from the db use htmlspecialchars,
https://www.php.net/manual/en/function.htmlspecialchars.php

Then you are ordinarily doing it wrong. You don’t replace anything.

OK, I saved it to the database. In the databse, I can see quotes, ellipses, etc.
When I pull it from the database and use either htmlspecialchars or htmlentities, the string still appears with invalid characters.

$con = mysqli_connect(‘localhost’, $loginName, $password,‘StarkHousePressMain’);
if (!$con) {
die(“Could not connect. Please check your password and try again later.”);
}
$result = mysqli_query($con,“SELECT * FROM testertext”);
$rowcount=mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result))
{
$new = htmlspecialchars($row[textin], ENT_QUOTES | ENT_SUBSTITUTE);
echo $new;
}

Can you provide a zip of everything I need to run it?

I have no idea how you are inserting it. What “invalid characters”?

I’ve created a zip file and granted you database access. I’ve also included a screen shot of what I’m getting back from the database. I really appreciate any help you can give! I’ve been going crazy over this!

Where would you like me to send the zip file?

I just tested your code and it outputs correctly. The only problem in helpget.php is this is missing quotes.

This $row[textin] should be $row[‘textin’]

I did not test helpput.php. I just entered the text string directly to the database.

Can I ask what browser you’re using? Maybe it’s a browser issue, since mine is still garbled. And did you use your own server for the database? Or mine? Thanks!

I used my own DB and Firefox.

What does “garbled” mean? Post a screenshot. Might be a Character set issue. DB should be using utf8mb4

It looks the same as the screenshot I sent you. I’m trying different char sets on the database to see if I can get it to work. I will most likely have to talk to the DB admin. I really appreciate all your help!

I didn’t notice the screenshot before. That would be an encoding problem. You are using “fancy quotes” aka “Smart quotes” and it is not supported by the charset you are using.

No it doesn’t. Do you see anything “garbled” in my screenshot? Look at the one you sent me again.

You were right. It’s an encoding issue. I’ll check with my DB admin in the next few days. Thanks again!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service