(PECL event >= 1.2.6-beta)
EventBuffer::search — Scans the buffer for an occurrence of a string
$what
[,
EventBufferPosition
$start
= NULL
[,
EventBufferPosition
$end
= NULL
]] )
Scans the buffer for an occurrence of the string
what
. It returns object representing the position of the string, or NULL if
the string was not found.
If the
start
argument is provided, it represents the position at which the search should
begin; otherwise, the search is performed from the start of the string. If
end
argument provided, the search is performed between start and end buffer
positions.
what
String to search.
start
Instance of EventBufferPosition representing start search position.
end
Instance of EventBufferPosition representing end search position.
Returns an instance of
EventBufferPosition
representing position of the first occurance of the string in the buffer,
or NULL if string is not found.
Example #1 EventBuffer::search() example
<?php
// Count total occurances of 'str' in 'buf'
function count_instances($buf, $str) {
$total = 0;
$p = new EventBufferPosition();
$buf->setPosition($p, 0, EventBuffer::PTR_SET);
$i = 0;
while (1) {
$p = $buf->search($str, $p);
if (!$p) {
break;
}
++$total;
$buf->setPosition($p, 1, EventBuffer::PTR_ADD);
}
return $total;
}
$buf = new EventBuffer();
$buf->add("Some string within a string inside another string");
var_dump(count_instances($buf, "str"));
?>
The above example will output something similar to:
int(3)