Search code examples
phpstring-comparison

Compare strings regardless of leading or trailing spaces


I want to check if the data is match between array and the string. Im trying to check if the string is equals to each other but the problem is the condition is returning false and both value of $subex2[1] and $subdat is IT100. I think the problem is you can't compare an array to a string. Can someone help me about this?

here's the code

$subex = $objPHPExcel->getActiveSheet()->getCell('D8')->getValue();
$subex2 = explode(":", $subex);

$q = "select * from class where id='$id'";
$r = mysql_query($q);
$data = mysql_fetch_array($r);
$subdat = $data['subject'];

if ($subdat == $subex2[1]) {
    echo "Data matched";
} else {
    echo "Data doesn't matched";
}

Solution

  • As mentioned in the comments. One value has an space before it. You can solve this kind of problems like this:

    if(trim($subdat) == trim($subex2[1])) {
        echo "Data matched";
    } else {
        echo "Data doesn't matched";
    }
    

    for case sensitive issue, this trick should apply.

    if(strtolower(trim($subdat)) == strtolower(trim($subex2[1]))) {
            echo "Data matched";
        } else {
            echo "Data doesn't matched";
        }