2011-01-27 1 views
2

아래 테스트는 $ 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

0 무엇을 반환 ... 그것은 그 건초 더미 나에게 보인다 바늘없는 같은 또는 바늘이 또는($post->post_title == "") 발견되지 않는 이유는 무엇입니까? 'stripos()'또는'my_test()'? – BoltClock

+0

'my_esc'와'my_esc2' 함수는 무엇을합니까? 특히이 주석이 주어지면 함수의 출력은 무엇입니까? 그런 식으로''''' "을 없애는 실수입니다 (대신'trim'을 사용하십시오). – fredley

+0

echo $ pos; (0을 반환) –

답변

3

stripos이 일치하는 경우 찾은 문자열의 위치는 0 0으로 반환 할 예정이다.

그러나 연산자는 !== 연산자를 사용하므로 테스트에서는 true을 반환해야합니다 (단, return ($pos !== false) 만 사용할 수 있습니다).

진술에 도달 했습니까? return 문 바로 전에 $haystack$needle을 반향시킬 수 있습니까?

+0

도 나에게 의미가 있습니다. 추가적인 흔적을 남겨주세요 ... –