Geppettvs D'Constanzo Geppettvs D'Constanzo - 8 months ago 39
PHP Question

How can I input accent words in a PHP script interaction?

I am writing a PHP script which is intended to run in a Linux Terminal in order to drop variable results in a text file.

Everything seems to be working fine but when I run the script and it shows the input lines in a terminal I can't write accent words including the single accent tilde (

) and accented letters like
á é í ó ú

Here's the portion of code I am using to accept the user input:

$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);

A screenshot of the script running in a terminal is shown here:

enter image description here

Please notice that the text should read
Este texto debería tener acentos pero en la terminal no está aceptando ningún caracter con tilde.
, nevertheless it is not showing any accent in the text.

I have already tried using the
<?php header("Content-Type: text/html; charset=utf-8"); ?>
and some other tricks for charsets in order to make the
to accept accent's with no success. I have also tried changing
default charset and no success so far.

I can write the accent letters in a terminal without problem as seen in this screenshot:

enter image description here

After inserting
as suggested, no luck yet.

enter image description here
enter image description here

Perhaps something's wrong with my terminal but that made me think this should work on a tty terminal, or not? So I tried and I got the same results.

enter image description here

Any help is appreciated.


EDITED enter image description here

The problem is that default php package on Ubuntu 14.04 uses libedit which was originally written for BSD. It has a bug and the workaround requires replacing libedit with readline.

Here are the bug reports:



I created a Debian package with corrected build. I works successfully on Ubuntu 14.04 and solves the problem with accent letters.

  1. cd /tmp
  2. git clone
  3. cd ./php5
  4. dpkg -i -B --force-depends --force-overwrite php5_5.5.9+dfsg-1ubuntu4.17_all.deb
  5. php5enmod readline
  6. dpkg -i php5-readline_5.5.9+dfsg-1ubuntu4.17_amd64.deb


In case of dependency errors, run : sudo apt-get -f install