为什么strpos在这个例子中失败了

时间:2013-09-19 21:21:52

标签: php strpos strstr

我想清理url地址,但首先我要检查字符串中是否应该替换。我想使用strpos,因为它更快,但它无法检测到字符串。

$txt = 'http://www.youtube.com/watch?v=aWFmXFFjJfw';

if(strpos($txt, 'http://www.')){
echo 'true strpos'; // not shows
}

if(strstr($txt, 'http://www.')){
echo 'true strstr'; // show
}

另外

$match = strpos($txt, 'http://www.');
var_dump($match); // int(0);

1 个答案:

答案 0 :(得分:7)

因为它等于0,等于假。

改为使用:

if(strpos($txt, 'http://www.')!==false){
    echo 'true strpos'; //shows
}
相关问题