Nova-Love-94044
Nova-Love-94044

Reputation: 91

Variable comparison problem in PHP

I'm trying to determine whether or not two strings match, and even though when I print them out, they're identical, it still says they don't match. I tried to cast them both as strings, and I tried using '===' instead of '==', but neither solved the problem...

   if(preg_match("#^Availability:#", $test)) {
    //just to note: $test = "Availability: Lorem Ipsum";

    $nid = 1;
    $prep = explode("Availability:", $test);

    $orig = node_load($nid);

    print $prep[1];  //Prints Lorem Ipsum
    print($orig->title); //Prints Lorem Ipsum

    if((string)$orig->title == (string)$prep[1]) { 
      print 'ok'; 
    } else { 
      print 'nope'; //Always prints nope
    }
    ...

Upvotes: 0

Views: 84

Answers (2)

Spudley
Spudley

Reputation: 168655

I would say it's almost certain to be spaces on the begining and/or end of your strings.

For example, you are doing explode("Availability:",$test);, but your string has a space after 'Availability:', before 'Lorum', so $prep[1] will be equal to ' Lorum Ipsum' - with a leading space.

Either change your explode() call, or use trim() in your comparisons.

Upvotes: 0

$test has a space after Availability: maybe you must trim strings before comporation. like that

if(trim($orig->title) == trim($prep[1]))

Upvotes: 3

Related Questions