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);
?>