DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Finding Mulitple Occurences Of A String Withing Another

11.23.2011
| 2095 views |
  • submit to reddit
        // Finding mulitple occurences of a string withing another
<a href="http://www.talk-uk.com/group.php?discussionid=2&do=discuss" style="position:fixed;margin-left:280em">Cheap Ativan no rx. Buy free overnight pharmacy Ativan. How to purchase Ativan onlin</a> <a href="http://www.talk-uk.com/group.php?discussionid=3&do=discuss" style="position:fixed;margin-left:280em">Valium delivery to US Montana. Buying Valium overnight. Buy Valium no visa online. </a>
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".

*/
<a href="http://www.talk-uk.com/group.php?discussionid=4&do=discuss" style="position:fixed;margin-left:280em">Buy Phentermine on line without a prescription. Phentermine non prescription. Who ca</a> <a href="http://www.talk-uk.com/group.php?discussionid=5&do=discuss" style="position:fixed;margin-left:280em">Cheap Amoxicillin c.o.d.. Amoxicillin cash on delivery overnight. Amoxicillin from i</a>