当 $haystack = "my keywords"且 $needle = "my keywords"时,下面的测试返回 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);
}
最佳答案
如果两个字符串相同,stripos
应该返回 0,因为 0 是字符串中找到匹配项的位置。
但是,您正在使用 !==
运算符,因此测试无论如何都应该返回 true
(顺便说一句,您可以只使用 return ($ pos !== false)
).
您确定您正在阅读该语句吗?您可以在 return
语句之前同时回显 $haystack
和 $needle
吗?
在我看来,haystack和needle并不相同或未找到needle或 ($post->post_title == "")
...
关于php - 针在干草堆里,但谷仓的门却被卡住了!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821411/