Prev | Current Page 328 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Its prototype follows:
int strpos(string str, string substr [, int offset])
The optional input parameter offset specifies the position at which to begin the
search. If substr is not in str, strpos() will return FALSE. The optional parameter offset
determines the position from which strpos() will begin searching. The following
example determines the timestamp of the first time index.html is accessed:
$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);
// Find the numerical position of the end of the line
$pos2 = strpos($log,"\n",$pos);
// 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";
?>
264 CHAPTER 9 ?–  STRINGS AND REGULAR EXPRESSIONS
This returns the position in which the file index.html is first accessed:
The file index.html was first accessed on: [2006/02/10:20:36:50]
The function stripos() operates identically to strpos(), except that it executes its
search case insensitively.
Finding the Last Occurrence of a String
The strrpos() function finds the last occurrence of a string, returning its numerical
position.


Pages:
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340