Upload file . txt removing php accents

Asked

Viewed 460 times

1

I need somehow to upload a file. txt and all the text is stored in a array and then run this array and remove all accents from the text, after that generate the file . txt without the accents ? (Take the accents from the contents inside the.txt file).

  • Stored in an array? What do you mean? One line per input?

  • It’s like, I don’t know if array but I need to read the file to get the right accents ? Is there any other way ?

  • But want to upload right first?

  • Yeah, that’s right.

1 answer

3


Do so:

<form enctype="multipart/form-data" method="POST">
    <input type="file" name="txt">
    <input type="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $file = $_FILES['txt'];
    $dir = 'tests/'; // ajustar nome diretório destino
    $fileName = 'new_txt.txt'; // ajustar nome do ficheiro
    move_uploaded_file($file['tmp_name'], $dir.$fileName);
    $text = utf8_encode(file_get_contents($dir.$fileName);
    $search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u");
    $replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u");
    $new_text = str_replace($search, $replace, $text);
    $file = fopen($dir.$fileName, 'w');
    fwrite($file, $new_text);
    fclose($file);
    echo 'VELHO TEXTO:<br><br>' .$text. '<br><br>';
    echo 'NOVO TEXTO:<br><br>' .file_get_contents($dir.$fileName);
}

Adjust where I commented "adjust..."

  • It is working to upload and create the new file . txt however is not removing the accents.

  • Made a mistake?

  • No, only in the echo the accents get a "?". So Lu s Lopes Corr a.

  • edited, compare the two and say @Kevin. F

  • It’s still the same with the "?". The old and the new.

  • Weird @Kevin. F ... I have a file with just what’s on top, no more and no less, and it runs right

  • @Kevin. F is sure that $dir.$filename in your case also refers to the destination/path of the new file without the accents?

  • Yes in mine it was like this $dir = 'ArquivoGerado/'; and $fileName = 'NewArq.txt'; I just changed these two lines.

  • And is the file still there with accents? @Kevin. F

  • Chat @Kevin. F, the link in the comment above

  • 2

    Good answer, but I recommend searching for duplicates before answering and using the lock or flag. For example http://answall.com/search?tab=votes&q=[php]+remove+accents

  • You’re absolutely right @Guilhermenascimento, I don’t have that habit yet... I always assume that this has to come from those who ask the question

Show 8 more comments

Browser other questions tagged

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