1

I have this code to parse through a remote TXT file. It works fine on a remote server which runs on unix, but on Windows 10 Apache, it does not get to the echo <hr>Reached here...<hr>; line.

This is the code:

<?php
if (!file_exists('unicode-13.txt')) {
    file_put_contents('unicode-13.txt', file_get_contents('https://unicode.org/Public/emoji/13.0/emoji-test.txt'));
}

$blocks = explode(PHP_EOL.PHP_EOL, file_get_contents('unicode-13.txt'));

// var_dump($blocks);

unset($blocks[0]);

$emoji = [];

foreach ($blocks as $chunk) {

    echo "<hr>Reached here...<hr>";

    $top = explode(PHP_EOL, $chunk)[0];

    if (substr($top, 0, strlen('# group:')) == '# group:') {

        $group = trim(str_replace('# group:', '', $top));

    } elseif (substr($top, 0, strlen('# subgroup:')) == '# subgroup:') {

        $lines = explode(PHP_EOL, $chunk);
        unset($lines[0]);

        foreach ($lines as $line) {

            $subgroup = trim(str_replace('# subgroup:', '', $top));
            $linegroup = explode(';', $line);
            $parts = explode('#', $linegroup[1]);
            $icon = explode(' ', trim($parts[1]), 2);

            $var_codepoint =    trim($linegroup[0]);
            $var_status =       trim($parts[0]);
            $var_sub_group =    trim($subgroup);
            $var_emoji =        trim($icon[0]);
            $var_emojiname =    trim($icon[1]);

            echo "var_codepoint: " . $var_codepoint . "<br />";
            echo "var_status: " . $var_status . "<br />";
            echo "group: " . $group . "<br />";
            echo "var_sub_group: " . $var_sub_group . "<br />";
            echo "var_emoji: " . $var_emoji . "<br />";
            echo "var_emojiname: " . $var_emojiname . "<hr />";

        } // line
    } // subgroup
} // foreach
?>

I get no errors on Windows - I have verified that the $blocks array is populated on the windows test, as a var_dump($blocks); displays the array.

However, it seems that on windows, none of the code beyond unset($blocks[0]); is fired / reached.

I get no errors in the windows php error log, so I am not sure what might be going on.

4532066
  • 2,042
  • 5
  • 21
  • 48
  • What have you tried to debug that problem? Running a debugging session through XDebug is not that difficult, and even a `var_dump` right before that line you assume to not work should be simple – Nico Haase Feb 08 '20 at 20:14

1 Answers1

3

Using PHP_EOL is wrong here. It will be \n on Unix and \r\n on Windows, but the contents of the remote file don't depend on your operating system — the file uses \n regardless. So just use "\n" in your code instead of PHP_EOL, and it will work. Currently it fails because the file doesn't contain any instance of "\r\n\r\n", so the first explode only returns one "block" containing the whole file, and then you throw away the first block, leaving nothing.

hobbs
  • 223,387
  • 19
  • 210
  • 288