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条回答
冷血范
2楼-- · 2019-01-12 04:02

In PHP there is a pretty good function utf8_encode() to solve this issue.

echo utf8_encode("Résumé");

//will output Résumé instead of R�sum�

Check the official PHP page.

查看更多
神经病院院长
3楼-- · 2019-01-12 04:03

So I try htmlspecialchars() or htmlentities() which outputs <p>Résumé<p> and the browser renders <p>Résumé<p>.

If you've got it working where it displays Résumé with <p></p> tags around it, then just don't convert the paragraph, only your string. Then the paragraph will be rendered as HTML and your string will be displayed within.

查看更多
等我变得足够好
4楼-- · 2019-01-12 04:10

This works for me. Try this one before the start of HTML. I hope it will also work for you.

<?php header('Content-Type: text/html; charset=iso-8859-15'); ?>
<!DOCTYPE html>

<html lang="en-US">
<head>

查看更多
登录 后发表回答