【发布时间】:2014-07-10 07:28:07
【问题描述】:
$target 是一个字符,我试图在 $line 中找到该字符的最后一次出现。即使我确定 $target 确实存在于 $line 中的某个索引处,我也会为每个输出得到 -1。
$fh = fopen($someFile, "r");
while (!feof($fh)) {
$test = fgets($fh);
$words = explode(",", $test);
$line = $words[0];
$target = $words[1];
$answer = strrpos($line, $target);
if ($answer !== false) {
echo $answer;
}
else echo -1;
echo "\n";
}
此代码为每个单个值返回 -1。如果我在 strrpos 函数中将 $line 更改为 $test ,它每次都可以找到索引。我检查了 $line 以确保它不为空,它实际上是字符串的第一部分。为什么这不起作用?
【问题讨论】:
-
你在哪里设置
$answer? -
可能是
$answer = strrpos ($words[0], $words[1]);... -
@MarcoS 好吧,如果他忘记实际这样做,他确实总是会得到 -1
-
好吧,即使是 -1,如果没有找到匹配项,strrpos 将返回 FALSE...
-
@MarcoS 他总是会得到-1,因为他只是用
else echo -1;声明if()永远不会是真的。