Advanced "Is it a tag" function

Alright so I’m creating a web application involving tagging, and it generates tags based off of the description of an item. However I’ve encountered the obvious error of video CARds being categorized as an auto item due to the tag car being found. So I made a function to check if it’s really a word - Now it takes the needle and the 1 space to each side of it, and determines if it’s a word - That part works fine. However it only does this to the first occurence, so if “the card has a car in it”, I want the car tag to still be found - Meaning I have to loop through all instances of the needle.

My initial logic: Take the count of all instances of the needle, run a for loop that runs the amount of the count, and each time it runs it, it cuts the string down to remove that part, so it works on the next, however it’s not returning anything.

Thoughts?
Code:

function isItReallyATag($needle, $haystack)
	{
				// How many times does the needle show up in the haystack?
				$needleCount = substr_count($haystack, $needle);
				$needleArray = array();
				echo $needleCount;
				
				for ($i=0; $i<$needleCount; $i++)
					{
						// Trim spaces from needle to begin with
						$needle = trim($needle);
						
						// Determine if the needle is in the haystack in any way shape or form
						if (stristr($haystack, $needle)==FALSE)
							{
								return false;
							}
						
												// If it is in the haystack, we continue 
						
						// Set variables for the position of the start and end of the needle
						// We include 1 on each side to check if it's a word
						$needleStart = stripos($haystack, $needle);
						$needleStart -= 1;
						#echo 'start'.$needleStart.'<br />';
						// The end is the start + the length of the needle + 1
						$needleEnd = $needleStart + strlen($needle) + 1;
						#echo 'end'.$needleEnd.'<br />';
						
						// Variable to extract the needle from the haystack, with one character on each end
						//substr(
						$needleAndSurrounding = substr($haystack, $needleStart, $needleEnd);
						#echo 'needle+surrounding:'.$needleAndSurrounding.'<br />';
						
						// Let's find out whats on each side
						$leftSide = substr($needleAndSurrounding, 0, 1);
						#echo 'Left Side:'.$leftSide.'<br>';
						
						// Analyze the left hand side, if it doesn't have a space
						// on the left hand side, assume it's not a word
						if ($leftSide!=' ')
							{
								return false;
							}
							
						// Let's take a look at the right side, if it has a 
						// space, comma, or period, we'll consider it an actual occurence of the needle
						//substr(
						$rightSide = substr($needleAndSurrounding, strlen($needle)+1, 1);
						#echo 'Right Side:'.$rightSide.'<br>';
						
						switch($rightSide)
							{
								case ' ':
								break;
								
								case '.':
								break;
								
								case ',':
								break;
								
								default:
								return false;
								break;
							}
		
								
							
							
						// If the script reaches here, then we decide it is an actual word!
						// Let's return it trimmed with nothing on each side, the word alone.
						$needleArray[$i] = trim($needle);
						# return true;
						
						// Trim the initial haystack to not include the first occurence of the needle
						// this way we can find the other occurences
						$haystack = str_replace(trim($needle), '', $haystack);
					}
				return implode(', ', $needleArray);
			
	}
	
	 $description = "hair is thair with the hair products of chairs.";
	
	
	echo isItReallyATag('hair', $description);
	
	
	
?>

str_replace(), preg_replace(), explode(), and then foreach() might be a better way to extract words. Then use an associative array to keep track of how many times a word is used.

I messed around with it, and after a while finally got it to work, below is the working code if anyone is interested:

<?php

## Purpose of function is to find a word like CD, within a larger 
## string and make sure it isn't part of another word, like LCD.
## This will allow us to find tags in descriptions easier.


function isItReallyATag($needle, $haystack)
	{
				// Set the needle and haystack to lowercase to reduce case sensitivity issues
				$needle = strtolower($needle);
				$haystack = strtolower($haystack);
				
				// Set haystack to have a space before & after it to avoid negative string positions
				$haystack = ' '.$haystack.' ';
		
				// How many times does the needle show up in the haystack?
				$needleCount = substr_count($haystack, $needle);
				$needleArray = array();
				
				// Run through each instance of the needle
				for ($i=0; $i<=$needleCount; $i++)
					{
						// Trim spaces from needle to begin with
						$needle = trim($needle);
						
						// Determine if the needle is in the haystack in any way shape or form
						if (stristr($haystack, $needle)==FALSE)
							{
								// We replace our haystack with a new haystack excluding what we just searched for
								$haystack = str_replace($leftSide.$needle.$rightSide, '', $haystack);
								// We hit continue to skip ONLY this iteration of the needle instance
								continue;
							}
						
						################### NEEDLE IS FOUND IN HAYSTACK --- CONTINUE #################
						
						// Set variables for the position of the start and end of the needle
						// We include 1 on each side to check if it's a word
						$needleStart = stripos($haystack, $needle);
						$needleStart -= 1;
						
						// The end is the start + the length of the needle + 1
						$needleEnd = $needleStart + strlen($needle) + 1;
						
						// Variable to extract the needle from the haystack, with one character on each end
						$needleAndSurrounding = substr($haystack, $needleStart, strlen($needle)+2);
						
						// Define what is in that space one character to the left and right of the needle
						$leftSide = substr($needleAndSurrounding, 0, 1);
						$rightSide = substr($needleAndSurrounding, strlen($needle)+1, 1);
	
						// Analyze the left hand side, if it doesn't have a space
						// on the left hand side, assume it's not a word
						if ($leftSide!=' ')
							{
								// We replace our haystack with a new haystack excluding what we just searched for
								$haystack = str_replace($leftSide.$needle.$rightSide, '', $haystack);

								// Skip this needles iteration to move onto the next occurence, this one is invalid
								continue;  
							}

						
						// Analyzing the right side to see if it contains a space, comma, or period
						// If so, keep going with the function, it's valid -- Besides that it's invalid,
						// set thisPass to failure so it will skip the iteration
						if ($rightSide==' ')
							{
								$thisPass = 'success';
							} elseif ($rightSide=='.')
								{
									$thisPass = 'success';
								} elseif ($rightSide==',')
									{
										$thisPass = 'success';
									} else {
										$thisPass = 'failure';
									}
						
						// Check if thisPass has been considered an invalid Tag, if so,
						// break the iteration, as usual :-/
						if ($thisPass=='failure')
							{
								$haystack = str_replace($leftSide.$needle.$rightSide, '', $haystack);
								continue;
							}
						
						########################## NEEDLE IS CONSIDERED A VALID TAG ##########################
						
						// If the script reaches here, then we decide it is an actual tag!
						// Let's add it to our array, trimmed with nothing on each side, the word alone.
						$needleArray[$i] = trim($needle);
						// Even if it's successful, we need to remove that part from the array to find other potential occurences
						$haystack = str_replace($leftSide.$needle.$rightSide, '', $haystack);


					} ############# for ($i=0; $i<=$needleCount; $i++)


				// If our array of needles are found TO BE VALID TAGS is at least 1 - Meaning it found a tag
				// We return the array -- Otherwise, return false;
				if (count($needleArray)==0)
					{
						return false;
					} else {					
						// Remove duplicate values of the tag in our array
						$needleArray = array_unique($needleArray);
						// Return a comma seperated version of our array
						return implode(', ', $needleArray);
					}
			
	} ############# function isItReallyATag($needle, $haystack)
		
	
?>

There’s most likely a much more efficient way to do this, but oh well. :-/