php - 如何在 PHP 脚本交互中输入重音词?

标签 php linux terminal ubuntu-14.04 command-line-interface

我正在编写一个 PHP 脚本,该脚本旨在在 Linux 终端中运行,以便将变量结果放入文本文件中。

一切似乎都运行良好,但是当我运行脚本并在终端中显示输入行时,我无法编写重音词,包括单个重音波浪号 ( ´ ) 和重音字母,如 á é í ó ú .

这是我用来接受用户输入的代码部分:

<?php
$cuantos = readline("Cuantas veces? ");
$cadena = readline("Escriba el Texto: ");
$separar = readline("Incluir separadores? ");
$separar = strtoupper($separar);
$archivar = "";
for ($x = 1; $x <= $cuantos; $x++) {
        if ($separar=="S") { $archivar = $archivar . "Texto ".$x."\n";}
        if ($separar=="S") { $archivar = $archivar . "----------------------------------\n";}
        echo  $archivar = $archivar . $cadena\n;
        if ($separar=="S") { $archivar = $archivar . "==================================\n";}
}
file_put_contents('textos.txt', $archivar);
?>

此处显示了在终端中运行的脚本的屏幕截图:

enter image description here

请注意文本应显示为 Este texto debería tener acentos pero en la terminal no está aceptando ningún caracter con tilde. ,但它在文本中没有显示任何重音。

我已经尝试使用 <?php header("Content-Type: text/html; charset=utf-8"); ?>和一些其他的字符集技巧来制作 readline接受口音没有成功。我也试过改变 php.ini默认字符集,到目前为止没有成功。

如屏幕截图所示,我可以毫无问题地在终端中输入重音字母:

enter image description here

插入后 mb_internal_encoding("UTF-8");正如建议的那样,还没有运气。

enter image description here enter image description here

也许我的终端出了点问题,但这让我认为这应该可以在 tty 终端上运行,还是不行?所以我尝试了并且得到了相同的结果。

enter image description here

感谢任何帮助。

最佳答案

已编辑 enter image description here

问题是 Ubuntu 14.04 上的默认 php 包使用最初为 BSD 编写的 libedit。它有一个错误,解决方法需要用 readline 替换 libedit。

错误报告如下:

  1. https://mail-index.netbsd.org/tech-userlevel/2011/01/26/msg004486.html
  2. https://bugs.launchpad.net/ubuntu/+source/libedit/+bug/816758

解决方法:

我创建了一个具有更正版本的 Debian 软件包。它在 Ubuntu 14.04 上成功运行,并解决了重音字母的问题。

  1. cd/tmp
  2. git 克隆 https://github.com/mitghi/php5
  3. cd ./php5
  4. dpkg -i -B --force-depends --force-overwrite php5_5.5.9+dfsg-1ubuntu4.17_all.deb
  5. php5enmod 读取行
  6. dpkg -i php5-readline_5.5.9+dfsg-1ubuntu4.17_amd64.deb

注意:

如果出现依赖项错误,请运行:sudo apt-get -f install

关于php - 如何在 PHP 脚本交互中输入重音词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38167837/

相关文章:

php - 安全漏洞?

php - 如何使用 Laravel 4 将对象插入到 MongoDB 文档中

php - 使用php在目录中查找文件

c - 除 gcc 外,我可以使用哪个程序在 Linux 中进行调试?

windows - 线程 ID 与线程句柄

bash - brew 医生说 "Error:/usr/bin occurs before/usr/local/bin"如何修复?

PHP set_include_path 配置

linux - "fatal error: gnu/stubs-32.h: No such file or directory"但存在 stubs-64.h

terminal - 无法从终端复制制表符

macos - 从命令行格式化 Amazon EC2 实例列表