php – 奇怪的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函数做出了我没有得到的决定.我哪里做错了? (顺便说一下,我不需要这个在其他网站上工作.)
解决方法:
如果找不到字符串,则strpos()
可以返回false;如果在最开始找到字符串,则返回0.但是,在布尔检查中0的计算结果为false,因此您需要显式检查类型:
$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") !== false;
标签:strpos,php 来源: https://codeday.me/bug/20191003/1847748.html