아래 테스트는 $ haystack = "my keyword"및 $ needle = "my keyword"일 때 거짓 ($ pos = 0)을 반환합니다. 아마도 헛간에 빈 공간이 없어 내 stripos 테스트가 0을 반환하기 때문입니다.바늘은 건초 더미에 있지만 헛간 문은 닫혀 있습니다!
이 경우 비교를 바꾸려면 무엇을 변경해야합니까? 두 문자열이 같은 경우
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 무엇을 반환 ... 그것은 그 건초 더미 나에게 보인다 바늘없는 같은 또는 바늘이 또는
($post->post_title == "")
발견되지 않는 이유는 무엇입니까? 'stripos()'또는'my_test()'? – BoltClock'my_esc'와'my_esc2' 함수는 무엇을합니까? 특히이 주석이 주어지면 함수의 출력은 무엇입니까? 그런 식으로''''' "을 없애는 실수입니다 (대신'trim'을 사용하십시오). – fredley
echo $ pos; (0을 반환) –