Get hashtag from string and get into meta keywords and tags

元气小坏坏 提交于 2020-01-06 10:51:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!