Revision: 53413
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 22, 2011 18:29 by xmaestro
Initial Code
function multipleSubstringOccurences($string,$substring) { $string=strtolower($string); $substring=strtolower($substring); $matched_positions=array(); $sample_string_to_be_matched=$string; $string_to_be_found=$substring; $strlen_string_to_be_found=strlen($string_to_be_found); $matched_positions[]=strpos($sample_string_to_be_matched,$string_to_be_found); if(strpos($sample_string_to_be_matched,$string_to_be_found)) { $current_array_index=0; $cur_pos=strpos($sample_string_to_be_matched,$string_to_be_found)+$strlen_string_to_be_found; $still_finding=true; while($still_finding) { $sample_string_to_be_matched=substr($sample_string_to_be_matched,$cur_pos,strlen($sample_string_to_be_matched)); if(strpos($sample_string_to_be_matched,$string_to_be_found)) { $still_finding=true; $cur_pos=strpos($sample_string_to_be_matched,$string_to_be_found)+$strlen_string_to_be_found; $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=array(); $res_array=multipleSubstringOccurences($string_main,$string_to_find); /* Will return an array with all starting indexes of string "sometimes". */
Initial URL
Initial Description
I wrote this method just to find multiple occurences of a substring withing another and return indexed-positions that matched in an array.
Initial Title
Finding mulitple occurences of a string withing another
Initial Tags
Initial Language
PHP