简体   繁体   English

PHP中如何显示特殊字符

[英]How to display special characters in PHP

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> .我想打印或回显字符串,但输出将返回<p>R sum </p> So I try htmlspecialchars() or htmlentities() which outputs &lt;p&gt;R&eacute;sum&eacute;&lt;p&gt;所以我尝试htmlspecialchars()htmlentities()输出&lt;p&gt;R&eacute;sum&eacute;&lt;p&gt; and the browser renders &lt;p&gt;R&eacute;sum&eacute;&lt;p&gt;并且浏览器呈现&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:我正在使用 UTF-8:

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

What am I missing here?我在这里缺少什么? Why do echo and print output a for any special character?为什么echoprint输出任何特殊字符的 To clarify, the string is actually an entire HTML file stored in a database.澄清一下,该字符串实际上是存储在数据库中的整个 HTML 文件。 The real-world application is not just that one small line.实际应用不仅仅是那一行。

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 .虽然 UTF-8 字符集将在客户端上显示特殊字符,但另一方面,服务器可能不那么包容并且会将诸如àè类的特殊字符打印为

To make sure your server will print them correctly, use the ISO-8859-1 charset:要确保您的服务器正确打印它们,请使用ISO-8859-1字符集:

<?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):编辑(4年后):

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.这样做的原因是客户端(浏览器)通过响应header()被告知期待ISO-8859-1文本/html 文件。 (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 intact. (正如其他人提到的,你也可以通过更新.ini.htaccess文件来做到这一点。)然后,一旦浏览器开始将给定的文件解析到 DOM 中,输出将遵守任何<meta charset="">规则但保持你的 ISO 字符完好无损。

In PHP there is a pretty good function utf8_encode() to solve this issue.在 PHP 中有一个很好的函数utf8_encode()来解决这个问题。

echo utf8_encode("Résumé");

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

Check the official PHP page.检查官方 PHP 页面。

You can have a mix of PHP and HTML in your PHP files... just do something like this...你可以在你的 PHP 文件中混合使用 PHP 和 HTML ......只需做这样的事情......

<?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.这应该按照您想要的方式输出Résumé

If you don't have short tags enabled, replace the <?= $string ?> with <?php echo $string; ?>如果您没有启用短标签,请将<?= $string ?>替换为<?php echo $string; ?> <?php echo $string; ?>

So I try htmlspecialchars() or htmlentities() which outputs <p>Résumé<p> and the browser renders <p>Résumé<p>.所以我尝试 htmlspecialchars() 或 htmlentities() 输出 <p>Résumé<p> 并且浏览器呈现 <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.如果它在显示带有<p></p>标签的Résumé地方工作,那么不要转换段落,只转换你的字符串。 Then the paragraph will be rendered as HTML and your string will be displayed within.然后该段落将呈现为 HTML,您的字符串将显示在其中。

$str = "Is your name O\\'vins?"; $str = "你的名字是 O\\'vins 吗?";

// Outputs: Is your name O'vins? // 输出:你的名字是 O'vins 吗? echo stripslashes($str); echo stripslashes($str);

This works for me:这对我有用:

Create/edit .htaccess file with these lines:使用以下.htaccess行创建/编辑.htaccess文件:

AddDefaultCharset UTF-8
AddCharset UTF-8 .php

If you prefer create/edit php.ini :如果您更喜欢创建/编辑php.ini

default_charset = "utf-8"

Sources:资料来源:

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.

This works for me.这对我有用。 Try this one before the start of HTML.在开始 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>

最近遇到类似问题时,以下内容对我有用:

$str = iconv('iso-8859-15', 'utf-8', $str);

One of the best ways to do this is, change Collation in my SQL database.最好的方法之一是在我的 SQL 数据库中更改排序规则。

step 1: Go to the Mysql database第一步:进入Mysql数据库

step 2: Select the Text-based Row you want to get displayed (Eg., post or comments)第 2 步:选择要显示的基于文本的行(例如,帖子或评论)

step 3: edit the row and select collation as below.第 3 步:编辑该行并选择如下排序规则。

utf8mb4_unicode_ci

Make sure to change the collation of text rows whichever you want to display the special characters.确保更改要显示特殊字符的文本行的排序规则。

Sometimes htmlspecialchars_decode() or any other entity() doesn't convert your special chars to normal.有时 htmlspecialchars_decode() 或任何其他 entity() 不会将您的特殊字符转换为正常字符。 So, the above method will definitely help.所以,上面的方法肯定会有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM