PHP Won't Match String - Encoding Issue?

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:

// 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.


0 Answers PHP Won't Match String - Encoding Issue?