coding an instagram widget left me with a problem i can't solve by myself. I receive an array text which contains the image description and the tags, so like:
"text": "#Hashtag ! Lorem Ipsum dolor sit ament. #hash #myword #another #hashing",
and another array containing all the tags like:
"tags": [
"hashtag",
"hash",
"myword",
"another",
"hashing",
],
my plugin offers an option to select to show the description, the tags or the description AND the tags.
So therefore I have to remove the tags from the tags. So far so easy. First of all I take all the tags and extend them to start with a "#" so "hashtag" becomes "#hashtag" and save them to a new array (alltags).
Afterwards I am checking with a str_replace to replace all the hashtags which are also inside the text. It needs to be str_replace to be keysensitive.
$noTags = str_replace($alltags, "", $instagramText);
The Output of the text is
tag ! Lorem Ipsum dolor sit ament.
because the hash of "Hashtag" gets replaced by the "hash" hashtag.
I found this solution: PHP string replace match whole word which should work for me. But I have a whole array to check (dynamically) not only one word.
Now the question: How can I check for the WHOLE word not only part of the word, so that "hash" will not effect "hashtag"?
EDIT I WANT to show "Hashtag! Lorem Ipsum...." not only "! Lorem Ipsum...". This is why i went for key sensitive - the linking hashtags are all lowercase (hash), the tags which are used in sentences are not lowercase (Hashtag). So I could just delete the ones I don't need to build a correct sentence. For example it could also be "We #Want you" - so the hashtags delivered would be "want". If I replace all the output would be "We you". If I replace the whole word with keysensitive it would be "We #Want you". Hope you understand what I am trying to do.
Thank you!