Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

php - Get hashtag from string and get into meta keywords and tags

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...