-1

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!

Community
  • 1
  • 1
ThomasB
  • 402
  • 3
  • 14

1 Answers1

1

Try using RegEx to strip hashtags. You can use preg_replace function, e.g.

$tags = implode('|', $tagsArray);
$noTags = preg_replace('/\#(' . $tags . ')\b/i', '', $instagramText);

So actually it'll be:

$noTags = preg_replace('/\#(hashtag|hash|myword|another|hashing)\b/i', '', $instagramText)
// result:
" ! Lorem Ipsum dolor sit ament.    "

This way it strips all words (with \b word boundary metacharacter) starting with # char.

Hope it helps.

EDIT

Use preg_replace_callback to check if matched hashtag is starting with uppercase, like:

preg_replace_callback(
    '/\ ?#(' . $tags . ')\b ?/i',
    function($matches) {
        return is_first_uppercase($matches[1]) ? str_ireplace('#' . $matches[1], $matches[1], $matches[0]) : '';
    },
    $instagramText
);

You just need to implement is_first_uppercase function, for example like this

With:

"#Hashtag ! Lorem Ipsum #Dolor sit ament. #hash #myword #another #hashing"

It should produce:

"Hashtag ! Lorem Ipsum Dolor sit ament."
Community
  • 1
  • 1
chris
  • 36
  • 1
  • 4
  • Hi chris, your solution would help, but I forgot one major info I guess. I WANT to show "Hashtag! Lorem Ipsum...." not only "! Lorem Ipsum...". This is why i went for key sensitive - the hashtags are all lowercase, the tags which are used in sentences are NOT lowercase (Hashtag), the once which are just for linking are lowercase (hash). So I could just delete the ones I don't need to build a correct sentence. – ThomasB Sep 22 '16 at 09:55
  • @ThomasB ok, I forgot about that. You can remove the _case insensitive (i)_ switch at the end of the Regex and it should match also character case. But do you also want to replace '#Hashtag' with 'Hashtag'? – chris Sep 22 '16 at 10:11
  • To be honest, I have no idea what the code above should do? I am not familiar with replacements in php, so I don't understand why you have the str_ireplace matches[1] matches[1] matches[0]. BTW: I tried to insert it, with the edit it crashes my page while performing the first element. – ThomasB Sep 22 '16 at 12:31