0

I "solved" this myself: the text files must be encoded ANSI. But I don't know why, or why filter_var() didn't help, so I thought this would still be educational for myself and others.

I haven't learned databases yet so I've started my foray into templates / generated content for my photo gallery pulling info from txt files. I have categories.txt:

Architectural
Fashion
// etc

Then, within my template:

$Categories = file('shared/categories.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$This_Page = filter_var($Categories[1], FILTER_SANITIZE_STRING);

$Image_List = file('shared/'.$This_Page.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($Categories as $c) {
    if ($c == $This_Page) {
        echo 'test successful!';
    }
    else {
        echo 'test failed';
    }
}

This always works. However, if on line 2 I use Categories[0]:

If categories.txt is encoded UTF-8, shared/$This_Page.txt can't be found and the if returns "test failed", and var_dump() finds $This_Page to be 3 characters longer than it should be. Even with filter_var().

Worse, if categories.txt is encoded Unicode, the code fails from the very first line with "file() expects parameter 1 to be a valid path."

And only if categories.txt is encoded ANSI does it work correctly.

Lee Saxon
  • 457
  • 3
  • 14
  • 1
    You said you solved the problem but I don't see your solution! – Mason.Chase Jul 02 '17 at 17:48
  • Sorry, I just rewrote the grammar a little to make it more clear. The solution is just that the text files have to be encoded ANSI. – Lee Saxon Jul 02 '17 at 17:54
  • I read that file() does not support unicode reading, standard approach could be read file line by line and use `iconv()` or `mb_convert_encoding()` to convert the content. If you have a solution to your problem, you can post an answer at bottom. – Mason.Chase Jul 02 '17 at 18:39

0 Answers0