How to save php files with utf8 characters?

Asked

Viewed 126 times

0

When I name a php file with utf8 characters, for example 'çá.php' the local server does not open the file.

I really need names utf8 to name these files, but it is possible to do some kind of conversion. That is to take a name utf8, convert it to a file name that the server recognizes and save the file with the converted name. After that take the converted name and turn it into utf8 to use in the code. In other words, the process needs to be reversible.

What function or algorithm to perform this task?

Obs: I tried to use utf8_encode()/utf8_decode() but both generate filenames that the local server also does not recognize.

  • 1

    Catch ççáá.php and converting to ccaa.php is easy. See https://ideone.com/FMQ3DK but taking the converted name and turning it into utf8 is complicated. How to know if the name was ccáá.php or ççaa.php or ççáá.php or .......

  • I had to remodel the system to delete the second need. Now I just need to convert in ccaa, the link you posted covers the need. Thank you.

  • Since you deleted the second need, I will post the link code as a response.

1 answer

1

Since you only need to convert to ccaa, according to your comment, I will post the link code here

A secure accented character replacement implementation can be performed with the function strtr() call with only two arguments. strtr( $nomeArquivo, $indesejado_array );

$nomeArquivo = "ççáá.php";
$indesejado_array = array(    'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
                            'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
                            'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
                            'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
                            'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );

$nomeArquivo = strtr( $nomeArquivo, $indesejado_array );

example running on ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.