gpt4 book ai didi

php - 针在干草堆中,但谷仓门被卡住了!

转载 作者:行者123 更新时间:2023-12-04 06:36:26 25 4
gpt4 key购买 nike

当 $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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com