| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1966 人关注过本帖, 1 人收藏
标题:字符串函数的问题--offset偏移量
只看楼主 加入收藏
slfyeye
Rank: 1
来 自:上海
等 级:新手上路
威 望:1
帖 子:163
专家分:0
注 册:2006-3-5
收藏(1)
 问题点数:0 回复次数:0 
字符串函数的问题--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]]
搜索更多相关主题的帖子: offset 函数 字符 偏移 
2008-08-29 11:02
快速回复:字符串函数的问题--offset偏移量
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014430 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved