1

I'm trying to read file line by line and keeping them in array. But I have Turkish chars in that file and I tried everything to fix that problem but no result.

The file has content like:

  • Aksesuar ve Sarf / Pil / Taşınabilir Şarj Cihazı (PowerBank)
  • Aksesuar ve Sarf / Pil / Çinko
  • Aksesuar ve Sarf / Profesyonel Kaset /

You can see the Ş, ı chars. I see them as ? in array. What should I do?

Here is my code:

 public function getCategoriesByFile($fileUrl){
            $file = fopen($fileUrl,"r");
            $lines = array();
            $last = array();
            while(! feof($file))
              {
               $line= fgets($file);
              $line = $line; 
               array_push($lines , $line);
              }

            fclose($file);

            foreach($lines as $line){
                $parsedLines[] = preg_replace("/\s+/"," ",preg_split('/;/', trim($line)));
            }
            foreach($parsedLines as $parsed){
                $parse1 = rtrim($parsed[0] , " / ");
                $parse2 = rtrim($parsed[1] , " / ");
                array_push($last,array($parse1,$parse2));
            }
            return $last;
        }

the output like :

  }
  [2] => array(2) {
    [0] => string(64) "Aksesuar ve Sarf / Pil / Taşınabilir Åarj Cihazı (PowerBank)"
    [1] => string(0) ""
  }
  [3] => array(2) {
    [0] => string(31) "Aksesuar ve Sarf / Pil / Çinko"
    [1] => string(0) ""

Need your help guys. Thank you.

Qirel
  • 25,449
  • 7
  • 45
  • 62
sj_magento
  • 121
  • 1
  • 5
  • 1
    `$line = $line;`? =) Have you tried [utf8_decode()](http://php.net/manual/en/function.utf8-decode.php)/[utf8_encode()](http://php.net/manual/en/function.utf8-encode.php)? – syck Oct 09 '15 at 12:07
  • ahahah it s magical programing technique.No one knows but i do :D . i tried but no result :( – sj_magento Oct 09 '15 at 12:29
  • In the file you are reading from, are they properly encoded? It's just when using the function they break? @sj_magento – Qirel Oct 09 '15 at 12:33
  • in notepad at encoding tab selected "Encoding in UTF-8" and the file that i used this function above is also selected utf-8. – sj_magento Oct 09 '15 at 12:39
  • tried $line= utf8_encode(fgets($file)); array_push($lines,$line); tried $line = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $line); tried $line = iconv('ISO 8859-9', 'UTF-8', $line); tried $line = iconv('ANSI', 'UTF-8', $line); – sj_magento Oct 09 '15 at 12:45
  • You can take a look at this [`fpoen()` comment](http://php.net/manual/en/function.fopen.php#104325) - and also, you can check out [`mb_detect_encoding()`](http://www.php.net/manual/en/function.mb-detect-encoding.php) – Qirel Oct 09 '15 at 12:54
  • 3
    How are you looking at that result? I'd propose the output of PHP is perfectly fine, just whatever you use to look at that output doesn't handle UTF-8 correctly. No more, no less. – deceze Oct 09 '15 at 13:20
  • BTW, while we're at superfluous magic code, the entire first half of your code could be replaced with `$lines = file($fileUrl, FILE_IGNORE_NEW_LINES);`. – deceze Oct 09 '15 at 13:27
  • Agree with @deceze: Its probably a header encoding problem. See here: http://stackoverflow.com/questions/13517189/turkish-characters-are-not-displayed-correctly – toesslab Oct 09 '15 at 13:35
  • @deceze yes you are wright :D thank you . – sj_magento Oct 09 '15 at 15:44

1 Answers1

-1

try $line = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $line);

geryjuhasz
  • 184
  • 1
  • 15