【问题标题】:Why is this strstr() returning false?为什么这个 strstr() 返回 false?
【发布时间】:2013-08-07 02:22:42
【问题描述】:

我正在尝试使用 strstr() 查找字符串是否包含特定文本

$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr('/image/', $t));
exit;

但这给出了false。为什么它给fasle?如何解决?

【问题讨论】:

  • 你应该使用 strpos ,更快,更少资源
  • 明确证据表明绝地编写了 php.ini 文件。 “大海捞针,你必须找到一根针。”

标签: php strstr


【解决方案1】:

您的参数已反转(请参阅strstr)。这是正确的使用方法:

strstr($t, '/image/');

【讨论】:

  • 哦……为什么php总是和needlehaystack混在一起?
【解决方案2】:

您应该使用手册中的 strpos 更快、更少的资源和您的变量

<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
$findme   = '/image/';
$pos = strpos($t, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

【讨论】:

  • if(!$pos) 怎么样?这会省略=== 的需要吗?
  • 我接受了其他答案,因为那是我的问题的答案。但我正在使用你的解决方案。 +1
  • 没有积分!我失去的所有这些可爱的点:-; - 开个玩笑 ;-)
【解决方案3】:

试试这样吧

<?php
$t = "http://site.com/image/d2737cda28cb420c972f7a0ce856cf22";
var_dump(strstr($t, '/image/'));
exit;
?>

【讨论】:

    【解决方案4】:

    你可以查看函数的文档。

    验证语法。

    php/documentation/function.strstr.php

    正确的用法是:var_dump(strstr($t, '/image/'));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 2012-07-09
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多