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

标签 php

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

相关文章:

PHP:从类中调用类函数

php - MAMP - 所有 MySQL 查询返回 Null

PHP函数array_replace(),为什么参数是按引用传递的?

php - 我有一列包含逗号分隔值如何使用数组搜索这些值

PHP 试图在文本文件中找到特定的行并回显它

php - 在 PHPUNIT 中测试 API 且不泄露私有(private)凭证的最佳方法

php - 使用 Drupal 内容类型中的数据库自动完成字段

php - 如何在 PHP 中包含 MYSQL 的前 5 行表

php - 使用 tcp 套接字将数据从 php 发送到 c++

php - PHP/MYSQL创建表/insert数据