note 92849 modified in function.strpos by danbrown
| From: | danbrown@php.net | Date: | Tue, 11 Aug 2009 20:36:40 +0000 |
| Subject: | note 92849 modified in function.strpos by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-159344@lists.php.net to get a copy of this message | ||
A strpos modification to return an array of all the positions of a needle in the haystack
<?php
function strallpos($haystack,$needle,$offset = 0){
$result = array();
for($i = $offset; $i<strlen($haystack); $i++){
$pos = strpos($haystack,$needle,$i);
if($pos !== FALSE){
$offset = $pos;
if($offset >= $i){
$i = $offset;
$result[] = $offset;
}
}
}
return $result;
}
?>
example:-
<?php
$haystack = "ASD is trying to get out of the ASDs cube but the other ASDs told him that his
behavior will destroy the ASDs world";
$needle = "ASD";
print_r(strallpos($haystack,$needle));
//getting all the positions starting from a specified position
print_r(strallpos($haystack,$needle,34));
?>
--was--
A strpos modification to return an array of all the positions of a needle in the haystack
function strallpos($haystack,$needle,$offset = 0){
$result = array();
for($i = $offset; $i<strlen($haystack); $i++){
$pos = strpos($haystack,$needle,$i);
if($pos !== FALSE){
$offset = $pos;
if($offset >= $i){
$i = $offset;
$result[] = $offset;
}
}
}
return $result;
}
example:-
$haystack = "ASD is trying to get out of the ASDs cube but the other ASDs told him that his
behavior will destroy the ASDs world";
$needle = "ASD";
print_r(strallpos($haystack,$needle));
//getting all the positions starting from a specified position
print_r(strallpos($haystack,$needle,34));
http://php.net/manual/en/function.strpos.php