How to display special characters in PHP

2019-01-12 03:37发布

I've seen this asked several times, but not with a good resolution. I have the following string:

$string = "<p>Résumé</p>";

I want to print or echo the string, but the output will return <p>R�sum�</p>. So I try htmlspecialchars() or htmlentities() which outputs &lt;p&gt;R&eacute;sum&eacute;&lt;p&gt; and the browser renders &lt;p&gt;R&eacute;sum&eacute;&lt;p&gt;. I want it, obviously, to render this:

Résumé

And I'm using UTF-8:

header("Content-type: text/html; charset=UTF-8");

What am I missing here? Why does echo and print output a for any special character? To clarify, the string is actually an entire html file stored in a database. The real world application is not just that one small line.

9条回答
Summer. ? 凉城
2楼-- · 2019-01-12 03:44

The following worked for me when having a similar issue lately:

$str = iconv('iso-8859-15', 'utf-8', $str);
查看更多
Bombasti
3楼-- · 2019-01-12 03:49

You can have a mix of PHP and HTML in your PHP files... just do something like this...

<?php
$string = htmlentities("Résumé");
?>

<html>
<head></head>
<body>
<p><?= $string ?></p>
</body>
</html>

That should output Résumé just how you want it to.

If you don't have short tags enabled, replace the <?= $string ?> with <?php echo $string; ?>

查看更多
劫难
4楼-- · 2019-01-12 03:51

Try This

Input:

<!DOCTYPE html>
<html>
<body>

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>

<p>Converting &lt; and &gt; into entities are often used to prevent browsers from using it as an HTML element. <br />This can be especially useful to prevent code from running when users have access to display input on your homepage.</p>

</body>
</html>

Output:

This is some <b>bold</b> text.

Converting < and > into entities are often used to prevent browsers from using it as an HTML element. This can be especially useful to prevent code from running when users have access to display input on your homepage.
查看更多
叛逆
5楼-- · 2019-01-12 03:52

This works for me:

Create/edit .htaccess file with these lines:

AddDefaultCharset UTF-8
AddCharset UTF-8 .php

If you prefer create/edit php.ini:

default_charset = "utf-8"

Sources:

查看更多
Melony?
6楼-- · 2019-01-12 03:53

After much banging-head-on-table, I have a bit better understanding of the issue that I wanted to post for anyone else who may have had this issue.

While the UTF-8 character set will display special characters on the client, the server, on the other hand, may not be so accomodating and would print special characters such as à and è as and .

To make sure your server will print them correctly, us the ISO-8859-1 charset:

<?php
    /*Just for your server-side code*/
    header('Content-Type: text/html; charset=ISO-8859-1');
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"><!-- Your HTML file can still use UTF-8-->
        <title>Untitled Document</title>
    </head>
    <body>
        <?= "àè" ?>
    </body>
</html>

This will print correctly: àè


Edit (4 years later):

I have a little better understanding now. The reason this works is that the client (browser) is being told, through the response header(), to expect an ISO-8859-1 text/html file. (As others have mentioned, you can also do this by updating your .ini or .htaccess files.) Then, once the browser begins to parse that given file into the DOM, the output will obey any <meta charset=""> rule but keep your ISO characters in tact.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-12 03:59

$str = "Is your name O\'vins?";

// Outputs: Is your name O'vins? echo stripslashes($str);

查看更多
登录 后发表回答