问题
I'm creating a kind of a blog sharing link. Basically, for each link i would extract hashtag from post string and put it into a meta tags and keywords.
So, string normally is like this
#helloworld #test #video #youtube Bon Iver are the best daily surprise :D
Now, how can i get only hashtags and insert into a string like this ?
$hashtags = helloworld,test,video,youtube
And after insert hashtags in meta tag
<meta name="keywords" content="<? echo $hashtags ?>" >
<meta name="tags" content="<? echo $hashtags ?>">
Actually i used this script to remove "#" and put ","
$hashclear = preg_replace('/#([A-Za-z0-9]+)/', '$1,', $post);
How can i get these ? Any ideas ?
回答1:
Do you mean something like this:
$str = '#helloworld #test #video #youtube Bon Iver are the best daily surprise :D';
preg_match_all('/#([^\s]+)/', $str, $matches);
$hashtags = implode(',', $matches[1]);
var_dump($hashtags);
回答2:
$hashTag = "#helloworld #test #video #youtube Bon Iver are the best daily surprise :D";
$str = str_replace('#', '',
preg_replace('/(?:#[\w-]+\s*)+$/', '', $hashTag);
echo $str;
来源:https://stackoverflow.com/questions/19976571/get-hashtag-from-string-and-get-into-meta-keywords-and-tags