开发者

Needle is in haystack but barn door is jammed shut!

The test below returns false ($pos = 0) when $haystack = "my keyword" and $needle = "my keyword", presumably because my stripos test returns 0 since there is no empty space in the barn.

What do I need to change in the comparison to return true in this case?

function my_test($post){
    if($post->post_title == "") return false;
    $haystack = my_esc2($post->post_title);
    $needle = trim(my_getKeyword($post));
    $pos = stripos($haystack, $needle);
    if ($pos !== false) return true;
    //returns 0 when $needle and $haystack are the same exact phrase. but should return 1
}

function my_getKeyword($post)
{
    $myKeyword = get_post_meta($post->ID, '_my_keyword', true);
    if($myKeyword == "") $myKeyword = $post->post_title;
    $myKeyword = my_esc($myKeyword);
    return " ".$myKeyword;
}

function my_esc($str开发者_C百科, $quotation='"') {
    if ($quotation != '"' && $quotation != "'") return false;
    return str_replace($quotation, $quotation.$quotation, $str);
}

function my_esc2($str, $quotation='"') {
    if ($quotation != '"' && $quotation != "'") return false;
    return str_replace($quotation, '', $str);
}


If both strings are the same, stripos is supposed to return 0 as 0 is the position in the string where the match if found.

However, you are using the !== operator, so that test should return true anyway (by the way, you can just use return ($pos !== false)).

Are you sure you are getting to that statement, can you echo both $haystack and $needle right before the return statement?

It seems to me that haystack and needle are not the same or needle is not found or ($post->post_title == "")...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜