Andres SK
Andres SK

Reputation: 10974

Matching and removing twitter hashtags in a string

I need to remove the # character from hashtags only if the hashtag is anywhere but at the end of the string. Examples:

I'm going to #Disney this month! #WDW

I'm going to #Disney this month. #WDW #Orlando

I'm going to #Disney this month #WDW #Orlando

They need to be converted to:

I'm going to Disney this month!

I'm going to Disney this month.

I'm going to Disney this month

This code will remove all # characters:

function remove_hashtags($string){
    $result = preg_replace('/#([\w-]+)/i', '$1', $string);
    return $result;
}

...but before that, the hashtag (or group of hashtags) at the end of the string need to be removed.

Upvotes: 3

Views: 2342

Answers (1)

raina77ow
raina77ow

Reputation: 106443

How about this?

function remove_hashtags($string){
    return str_replace('#', '', 
        preg_replace('/(?:#[\w-]+\s*)+$/', '', $string));
}

I assume here you just need to remove all '#' characters - and not just the ones followed by "hashtag identifier candidates" (= matching the [\w-]+ pattern).

If that's your task, the code should be adjusted accordingly:

function remove_hashtags($string){
    return preg_replace('/#(?=[\w-]+)/', '', 
        preg_replace('/(?:#[\w-]+\s*)+$/', '', $string));
}

I've replaced capture group with lookahead here. Also, /i modifier is not required in both cases: \w special character covers both a-z and A-Z ranges.

Upvotes: 6

Related Questions