note 49238 added to function.strpos
| From: | admin at bwongar dot com | Date: | Fri, 21 Jan 2005 02:17:19 +0000 |
| Subject: | note 49238 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-83706@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49238
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49238&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49238&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49238&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49238&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49238&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49238&report=yes
Search -- http://master.php.net/manage/user-notes.php