作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当 $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);
}
最佳答案
如果两个字符串相同,则 stripos
应该返回 0,因为 0 是字符串中找到匹配项的位置。
但是,您使用的是 !==
运算符,以便测试应返回 true
无论如何(顺便说一下,你可以只使用 return ($pos !== false)
)。
你确定你正在接受那个声明,你能同时回应 $haystack
和 $needle
就在 return
之前陈述?
在我看来,大海捞针和针头不一样或 未找到针头 或 ($post->post_title == "")
...
关于php - 针在干草堆中,但谷仓门被卡住了!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821411/
我是一名优秀的程序员,十分优秀!