Reputation:
I have a string that contains text, and in several places there will be a twitter-style hashtag. I want to find them and create a seperate variable where all of them are seperated by spaces. I also want to convert all of the hashtags in the original string into links. Example:
$string = "Hello. This is a #hashtag and this is yet another #hashtag. This is #another #example."
after function:
$string_f = "Hello this is a <a href='#'>#hashtag</a> and this is yet another <a href='#'>#hashtag</a>. This is <a href='#'>another</a> <a href='#'>example</a>";
$tags = '#hashtag #another #example';
Upvotes: 2
Views: 420
Reputation: 59699
To find all of the hash tags, use a regex and preg_match_all()
, and do the replacement with preg_replace()
:
$regex = '/(#[A-Za-z-]+)/';
preg_match_all( $regex, $string, $matches);
$string_f = preg_replace( $regex, "<a href='#'>$1</a>", $string);
Then all of the tags are in an array in $matches[1]
:
$tags_array = $matches[1];
Then, convert that to a space-separated list with implode()
and array_unique()
:
$tags = implode( ' ', array_unique( $tags_array));
And you're done. You can see from this demo that $tags
and $string_f
are:
"#hashtag #another #example"
"Hello. This is a <a href='#'>#hashtag</a> and this is yet another <a href='#'>#hashtag</a>. This is <a href='#'>#another</a> <a href='#'>#example</a>."
For other characters in the hashtag (for example, digits), modify the $regex
appropriately.
Edit: However, this can be improved in efficiency if you use preg_replace_callback()
and a closure so you only have to execute the regex once, like so:
$tags_array = array();
$string_f = preg_replace_callback( '/(#[A-Za-z-]+)/', function( $match) use( &$tags_array) {
$tags_array[] = $match[1];
return "<a href='#'>" . $match[1] . "</a>";
}, $string);
$tags = implode( ' ', array_unique( $tags_array));
Upvotes: 6
Reputation: 3806
How about a little nifty regular expression?
preg_match_all("/#[\w\d]+/", $string, $matches, PREG_SET_ORDER);
unset($matches[0]);
$tags = implode(" ", $matches);
Upvotes: 0