duanlinpi0265 2009-11-24 08:46
浏览 25
已采纳

奇怪的strpos行为

This is my code:

<?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 . "
";
$soft404test = strpos($contloc, "http://www.uhasselt.be/404b.htm") ? true : false;
var_dump($soft404test);
?>

This is its output:

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)

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.)

  • 写回答

1条回答 默认 最新

  • doujiang2812 2009-11-24 08:50
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题