String compare not working

2019-07-09 05:32发布

问题:

I am trying to compare post titles in wordpress to avoid creating a post with a title that already exists.

foreach ($postnamearray as $value) 
{
    if($value === $titelzor)
    {
        echo' '.$value.' === '.$titelzor.' ';
    }
    else
    {
        echo' '.$value.' != '.$titelzor.' <br /> ';
    }
}

However, it's not working! When a match is supposed to be found, it comes back as (sorry for dutch text):

zovty, bedankt! != zovty, bedankt!

But it's supposed to come back as

zovty, bedankt! === zovty, bedankt!

So the script doesn't seem to detect it as having found a match. What did I do wrong?

回答1:

I guess one of the strings may contain trailing whitespace or newline characters, so try this:

if (trim($value) == trim($titelzor)) ...


标签: php wordpress