note 49238 deleted from function.strpos by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:14:17 +0000 |
| Subject: | note 49238 deleted from function.strpos by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196923@lists.php.net to get a copy of this message | ||
Note Submitter: admin at bwongar dot com
----
I created a useful function that returns an array with the positions within a string. For more info,
read the comments:
<?php
// Returns an array in this fashion:
// array(count => position)
function strpos_array($haystack, $needle){
$kill = 0; // Kills while loop when changed
$offset = 0; // Offset for strpos()
$i = 0; // Counter, not iterator
while ($kill === 0) {
$i++;
$result = strpos($haystack, $needle, $offset);
if ($result === FALSE) { // If result is false (no more instances found), kill the while loop
$kill = 1;
} else {
$array[$i] = $result; // Set array
$offset = $result + 1; // Offset is set 1 character after previous occurence
}
}
return $array;
}
?>