2012-09-02 58 views

回答

1

來自:http://www.php.net/manual/en/function.strpos.php#108426

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; 
} 

這將返回與OCCURENCES的位置的陣列。

+0

此功能是越野車,當*針* *是*草垛的前綴爲''strpos'returns 0'。 – Gumbo

+0

非常感謝,它的工作很好。 –

+0

沒問題 - 請將此答案標記爲已接受。 – Silox

0

從手冊中,評論中有這樣的功能。 strpos的

function strpos_recursive($haystack, $needle, $offset = 0, &$results = array()) {     
    $offset = strpos($haystack, $needle, $offset); 
    if($offset === false) { 
     return $results;    
    } else { 
     $results[] = $offset; 
     return strpos_recursive($haystack, $needle, ($offset + 1), $results); 
    } 
} 
0

第三個參數,有$偏移,您可以使用:

$positions_of_string = array(); 
$str_to_find = "string to find"; 
$str_length = strlen($str_to_find); 
$last_found = 0 - $str_length; 

while(false !== $last_found) { 
    $last_found = strpos($the_string, $str_to_find, $last_found+$str_length); 
    if(false !== $last_found) 
     $positions_of_strings[] = $last_found; 
}  
相關問題