English 中文(简体)
使用PHP在CLI/shell/终端中打印表情符号/外来字符
原标题:Print emoji/foreign characters in CLI/shell/Terminal using PHP
The bounty expires in 6 days. Answers to this question are eligible for a +50 reputation bounty. Aizzat Suhardi wants to draw more attention to this question.

当我在终端中运行包含<;?的脚本时;?php echo“?पीएचपी“;,它显示垃圾字符,而不是表情符号和外来文本。

具体地说,它显示lectƒÜÇαñ,αñÇañÅαñ。

但是,运行包含<code>console.log(“?पीएचपी“)是否正确显示表情符号和外语文本为?पीएचपी

使用PHP时,如何正确地回显/打印表情符号和外来文本,使其在CLI中按预期显示?

关于如何解决这个问题并让PHP在终端中正确显示表情符号和unicode文本,有什么建议吗?

该场景已使用Windows终端(Powershell 7)、cmd和GitBash(MINGW64)终端进行了测试

在我的windows终端中运行chcp会返回65001(即utf-8)。因此,终端本身被正确地配置为UTF-8。chcp的参考:https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers?redirectedfrom=MSDN

最小可复制性示例:

  1. Run chcp 65001 in Windows Terminal/cmd.
  2. Run chcp again to make sure it returns Active code page: 65001.
  3. Run the php script below(make sure extension=mbstring is enabled in php.ini):
<?php

$utf8_string = "?पीएचपी";
$detected_encoding = mb_detect_encoding($utf8_string);

echo "Detected encoding[$utf8_string]: " . $detected_encoding;
  1. I still got this displayed:
Detected encoding[🚀पीएचपी]: UTF-8
问题回答

您看到的问题可能与终端的编码设置有关,而不是PHP本身。您的终端需要支持并设置为使用UTF-8才能正确显示表情符号和外来文本。mb_detect_encoding函数正在检测字符串的编码为UTF-8,这是正确的。

为了验证PHP是否正确处理UTF-8编码的字符串,您可以将字符串写入一个文件,然后在您知道支持UTF-8的文本编辑器中打开该文件。如果文本在文本编辑器中显示正确,那么PHP正确地处理了UTF-8编码,问题可能与终端的设置有关。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...