字符串函数的问题--offset偏移量
下面一段是《PHP与MySQL5程序设计》中的字符串一章中的代码。主要介绍strpos()函数及其偏移量参数offset有些不明白。代码及问题如下:-----------------------------------------------------------
<?php
$substr = "index.html";
$log = <<< logfile
192.168.1.11:/www/htdocs/index.html:[2006/02/10:20:36:50]
192.168.1.13:/www/htdocs/about.html:[2006/02/11:04:15:23]
192.168.1.15:/www/htdocs/index.html:[2006/02/15:17:25]
logfile;
// what is first occurrence of the time $substr in log?
$pos = strpos($log, $substr);//此时echo $pos;会显示25,请问这是我标记的红色代码段的长度吗?并且位置是从0开始计数的吧?所以“index.html”首次出现的位置应该是25.
// Find the numerical position of the end of the line
$pos2 = strpos($log,"\n",$pos);//此时echo $pos2;会显示58,不清楚为什么是58?因为定义了偏移量offset为$pos,所以不是应该从index.html的字幕i的位置开始数(0,1,2...),得出32?这一点盼望高手解答下啊,谢谢!
// Calculate the beginning of the timestamp
$pos = $pos + strlen($substr) + 1;
// Retrieve the timestamp
$timestamp = substr($log,$pos,$pos2-$pos);
echo "The file $substr was first accessed on: $timestamp";
?>
-----------------------------------------------------------
整段代码运行结果为:The file index.html was first accessed on: [2006/02/10:20:36:50]
[[it] 本帖最后由 slfyeye 于 2008-8-29 11:03 编辑 [/it]]