0

I am currently having an issue with random strings in either variables or array values being lower cased periodically.

Here is an example of what I'm seeing with an array:

 $modules = array("Dashboard", "Calendar", "Maintenance", "Service Orders", "Quotes", "Contacts");

var_dump($modules);

Output:


    array(6) {
      [0]=>
      string(9) "Dashboard"
      [1]=>
      string(8) "Calendar"
      [2]=>
      string(11) "maintenance"
      [3]=>
      string(14) "Service Orders"
      [4]=>
      string(6) "Quotes"
      [5]=>
      string(8) "Contacts"
    }

I am doing an in_array() condition but it is not finding "Maintenance" due to it being "maintenance".

EDIT

Here is an example of what happens with passing a string through a method that tries to get the index in the $_REQUEST var:

class RequestHandle {

    /**
     * Get the parameter index of the request var
     * @param $index
     * @return string|array
     */
    public static function getParameter($index){
        $value = "";
        $data  = $_REQUEST;

        if(isset($data[$index]) && !is_array($data[$index])) {
            $value = trim($data[$index]);
        } else if(isset($data[$index]) && is_array($data[$index])) {
            foreach($data[$index] as $key => $value){
                $data[$index][$key] = $value;
            }

            $value = $data[$index];
        }

        return $value;
    }
}

$costs = RequestHandle::getParameter("SubLaborCosts");

Output:

    string(0) ""

The Request var is set and with data. When I do a print of $index in the getParameter method it will display "sublaborcosts", sometimes, and not find the index since "SubLaborCosts" =/= "sublaborcosts". In order for me to correctly grab data without missing anything, I had to modify the method to include:

$data  = array_change_key_case($_REQUEST);
$index = strtolower($index);

And this is even now and then, 80% of the time it is correct in showing exactly what I have set.

I've never seen this before and here is a list of items that are new to this happening that I think might have an effect... just can't find the solution.

  1. UTF-8 charset
  2. PHP 5.5
  3. intl extension
  4. Windows Server 2012

Any help or ideas would be greatly appreciated!

kandrews
  • 1
  • 3
  • This happen in arrays created by you? You set `$array[]='Maintenance'` and have 'maintenance'? – fusion3k Feb 12 '16 at 02:16
  • Yes, that is correct. – kandrews Feb 12 '16 at 02:30
  • I can't believe that PHP would just randomly change the characters that you assigned in a literal array like that. If that really happened, PHP would be practically unusable. There must be something in your application that's doing it. – Barmar Feb 12 '16 at 02:33
  • @Barmar I have spent many hours this week and last trying to find out why data was not being saving through form posts, but then 2 minutes without touching code, it would save properly. – kandrews Feb 12 '16 at 02:39
  • If PHP really did this, I'm sure someone would have noticed it before. But I can't find anything in google. – Barmar Feb 12 '16 at 02:43
  • Can you upload the full code of the script to pastebin.com? – Barmar Feb 12 '16 at 02:45
  • @Barmar Yeah, I've tried searching and can't find anything. That's why I'm kind of leaning towards it having something to do with the international extension, server, or charset. Maybe something isn't set correctly. – kandrews Feb 12 '16 at 02:46
  • That was my thought as well. But extensions aren't supposed to change the basic operation of PHP, they just define new built-in functions. If you don't actually call any of the `intl_XXX` functions, I don't see how it could affect it. – Barmar Feb 12 '16 at 02:48
  • @Barmar For the most part, that is all of the code, especially the array example, but I post more when I get back in my office. – kandrews Feb 12 '16 at 02:53
  • This seems like something we may not be able to help with here. It's something very peculiar about your configuration. – Barmar Feb 12 '16 at 02:55
  • @kandrews What happen inside `array_change_key_case()` function? – fusion3k Feb 12 '16 at 12:09
  • @fusion3k that method lower cases the array keys. This was added in after I found this error in order to force all indexes of request to match the passed string, which is also forced to lower. – kandrews Feb 12 '16 at 13:19

3 Answers3

0

About case insensitive in_array():

You can use preg_grep() (regular expression):

preg_grep( "/Maintenance/i" , $array );

The i at the end of pattern means 'case insensitive'.


fusion3k
  • 11,568
  • 4
  • 25
  • 47
0

@fusion3k's Answer solves the problem, but I'll throw this one in too.

function in_array_case_insensitive($needle, $haystack) 
{
    return in_array( strtolower($needle), array_map('strtolower', $haystack) );
}

As an aside: Your problem is weird, I've never seen that happen before!

Hiphop03199
  • 729
  • 3
  • 16
0

So I was able to get this corrected and stable by updating PHP from 5.5.32 to 5.6.18 and using icuxx56 instead of icuxx51 for the intl extension. I don't know if there is a known issue with icuxx51, but it would be something for the PHP devs to look at. Thanks for trying to help guys.

kandrews
  • 1
  • 3