PHP 8.4.24 Released!

Voting

: four plus four?
(Example: nine)

The Note You're Voting On

Tim
17 years ago
If you would like to find all occurences of a needle inside a haystack you could use this function strposall($haystack,$needle);. It will return an array with all the strpos's.

<?php
/**
 * strposall
 * 
 * Find all occurrences of a needle in a haystack
 *
 * @param string $haystack
 * @param string $needle
 * @return array or false
 */
function strposall($haystack,$needle){
    
    $s=0;
    $i=0;
    
    while (is_integer($i)){
        
        $i = strpos($haystack,$needle,$s);
        
        if (is_integer($i)) { 
            $aStrPos[] = $i; 
            $s = $i+strlen($needle); 
        }
    }
    if (isset($aStrPos)) { 
        return $aStrPos; 
    } 
    else { 
        return false; 
    }
}
?>

<< Back to user notes page

To Top