奇怪的strpos行为 [英] Odd strpos behavior
本文介绍了奇怪的strpos行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
<?php
$url = "http://www.uhasselt.be/collegeroosters/2009_2010_298_5_10.html";
$headers = get_headers($url, 1);
print_r($headers);
$contloc = $headers["Content-Location"];
echo "Content-Location: " . $contloc . "\n";
$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") ? true : false;
var_dump($soft404test);
?>
这是它的输出:
Array
(
[0] => HTTP/1.1 200 OK
[Content-Length] => 2030
[Content-Type] => text/html
[Content-Location] => http://www.uhasselt.be/404b.htm?404;http://www.uhasselt.be:80/collegeroosters/2009_2010_298_5_10.html
[Last-Modified] => Mon, 22 Aug 2005 07:10:22 GMT
[Accept-Ranges] => bytes
[ETag] => "88a8b68fe8a6c51:31c9e"
[Server] => Microsoft-IIS/6.0
[MicrosoftOfficeWebServer] => 5.0_Pub
[X-Powered-By] => ASP.NET
[Date] => Tue, 24 Nov 2009 08:40:25 GMT
[Connection] => close
)
Content-Location: http://www.uhasselt.be/404b.htm?404;http://www.uhasselt.be:80/collegeroosters/2009_2010_298_5_10.html
bool(false)
此行为是意外的.我以为我正在做的是通过查看HTTP标头中的Content-Location属性来检测软404. strpos函数做出我不知道的决定.我哪里做错了? (顺便说一下,我不需要在其他网站上使用它.)
This behavior is unexpected. What I thought I was doing is detecting soft 404's by looking at the Content-Location attribute in my HTTP headers. The strpos function makes decisions I don't get. Where did I go wrong? (I don't need this to work on other sites, by the way.)
推荐答案
strpos()
可以返回false找不到字符串,或者如果在最开始处找到该字符串,则为0.但是,在布尔检查中,0的计算结果为false,因此您需要显式检查类型:
strpos()
can return false if the string isn't found or 0 if the string is found at the very beginning. However 0 evaluates to false in a boolean check so you need to explicitly check the type:
$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") !== false;
这篇关于奇怪的strpos行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文