Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...