2011-01-27 41 views
2

当$ haystack =“my keyword”和$ needle =“my keyword”时,下面的测试返回false($ pos = 0),大概是因为我的stripos测试返回0,因为谷仓中没有空的空间。针在干草堆里,但谷仓门被堵塞了!

在这种情况下,我需要在比较中更改以返回true吗?

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, $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); 
} 
+0

什么返回0? `stripos()`或`my_test()`? – BoltClock 2011-01-27 20:23:21

+0

函数`my_esc`和`my_esc2`做了什么?特别是,该评论的功能输出是什么?以这种方式摆脱“`”`“是一个错误(改用`trim`) – fredley 2011-01-27 20:26:59

回答

3

如果两个字符串是相同的,stripos应该返回0 0是在比赛,如果找到字符串中的位置。

但是,您正在使用!==运算符,因此无论如何测试应返回true(顺便说一下,您可以使用return ($pos !== false))。

您确定您正在使用该语句吗?您是否可以在return声明之前回显$haystack$needle

在我看来,那草垛和针是不一样的针没有找到($post->post_title == "") ...