-3

I am doing a string comparison in php, but strcasecmp always outputs that the strings are not equal.

Here is my code:

    <?php

function multiexplode($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$owner = "Musica no tAatro: venha ja";
$owner_keys = multiexplode(array(".",":"," "),$owner);
$musica_c = array("musica","teatro");
$musica = 0;

  if(sizeof($owner_keys) > 1){
        foreach($musica_c as $key_c){
                foreach($owner_keys as $cat){
                    if(strcasecmp($cat,$key_c)==0){
                        ++$musica;
                    }else{
                        echo "do not match<br>"; 
                    }
                }
            }
        }
?>

Why does strcasecmp detect different strings?

PLNech
  • 3,087
  • 1
  • 23
  • 52
user3709132
  • 21
  • 1
  • 7

1 Answers1

1

strcasecmp is doing its work properly.

It is matching one pair of string which is Musica & musica. Just echo $musica; in the end. However, another string pair is different which is tAatro & teatro. That is why not matching.

It is expected behavior.

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
  • sorry, but strcasecmp is not doing its work properly because when i echo $musica outputs 0. And outputs on every comparison "do not match". – user3709132 Oct 27 '14 at 18:12
  • I am getting 1 by just running your PHP code with added `echo $musica;`. Try running code in http://phpfiddle.org/lite – Apul Gupta Oct 27 '14 at 18:16