php PHP中如何显示特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12699037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:03:28  来源:igfitidea点击:

How to display special characters in PHP

phpspecial-charactershtmlspecialchars

提问by Phil Tune

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:

我想打印或回显字符串,但输出将返回<p>R?sum?</p>. 所以我尝试htmlspecialchars()orhtmlentities()哪个输出&lt;p&gt;R&eacute;sum&eacute;&lt;p&gt;和浏览器呈现&lt;p&gt;R&eacute;sum&eacute;&lt;p&gt;。显然,我希望它呈现这个:

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 echoand printoutput 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.

我在这里缺少什么?为什么对任何特殊字符执行echoprint输出 a ??澄清一下,该字符串实际上是存储在数据库中的整个 HTML 文件。实际应用不仅仅是那一行。

回答by Phil Tune

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 字符集将在客户端上显示特殊字符,但另一方面,服务器可能不太适应并且会打印特殊字符,例如àand èas??

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

要确保您的服务器正确打印它们,请使用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-1text/html file. (As others have mentioned, you can also do this by updating your .inior .htaccessfiles.) 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.

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

回答by Vikas Kandari

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 页面。

回答by Justin Wood

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; ?>

回答by NightHawk

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. Then the paragraph will be rendered as HTML and your string will be displayed within.

如果你有它的工作在此它显示Résumé<p></p>它周围的标签,那么就没有转换的段落,只是你的字符串。然后该段落将呈现为 HTML,您的字符串将显示在其中。

回答by seantunwin

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

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

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

回答by Vincy Oommen

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

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

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

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

回答by quantme

This works for me:

这对我有用:

Create/edit .htaccessfile with these lines:

.htaccess使用这些行创建/编辑文件:

AddDefaultCharset UTF-8
AddCharset UTF-8 .php

If you prefer create/edit php.ini:

如果您更喜欢创建/编辑php.ini

default_charset = "utf-8"

Sources:

资料来源:

回答by Milan Gajjar

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.

回答by Hafiz Ameer Hamza

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

这对我有用。在开始 HTML 之前尝试这个。我希望它也对你有用。

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

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