/ Published in: PHP

I wrote this method just to find multiple occurences of a substring withing another and return indexed-positions that matched in an array.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function multipleSubstringOccurences($string,$substring) { $sample_string_to_be_matched=$string; $string_to_be_found=$substring; { $current_array_index=0; $still_finding=true; while($still_finding) { { $still_finding=true; $matched_positions[]=$matched_positions[$current_array_index]+strpos($sample_string_to_be_matched,$string_to_be_found)+$strlen_string_to_be_found; $current_array_index++; } else { $still_finding=false; } } } else { return false; } return $matched_positions; } //Usage $string_main="Life is sometimes hard. Sometimes it's delightful."; $string_to_find="Sometimes"; $res_array=multipleSubstringOccurences($string_main,$string_to_find); /* Will return an array with all starting indexes of string "sometimes". */
Comments
