PHP 8.5.0 Alpha 4 available for testing

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

mtroy dot student at gmail dot com
13 years ago
when you want to know how much of substring occurrences, you'll use "substr_count".
But, retrieve their positions, will be harder.
So, you can do it by starting with the last occurrence :

function strpos_r($haystack, $needle)
{
if(strlen($needle) > strlen($haystack))
trigger_error(sprintf("%s: length of argument 2 must be <= argument 1", __FUNCTION__), E_USER_WARNING);

$seeks = array();
while($seek = strrpos($haystack, $needle))
{
array_push($seeks, $seek);
$haystack = substr($haystack, 0, $seek);
}
return $seeks;
}

it will return an array of all occurrences a the substring in the string

Example :

$test = "this is a test for testing a test function... blah blah";
var_dump(strpos_r($test, "test"));

// output

array(3) {
[0]=>
int(29)
[1]=>
int(19)
[2]=>
int(10)
}

Paul-antoine
Malézieux.

<< Back to user notes page

To Top