从字符串中获取主题标签并进入元关键字和标签


Get hashtag from string and get into meta keywords and tags

我正在创建一种博客共享链接。基本上,对于每个链接,我会从帖子字符串中提取主题标签并将其放入元标签和关键字中。

所以,字符串通常是这样的

#helloworld #test #video #youtube Bon Iver are the best daily surprise :D

现在,我如何只获取主题标签并插入到这样的字符串中?

$hashtags = helloworld,test,video,youtube

并在元标记中插入主题标签后

<meta name="keywords" content="<? echo $hashtags ?>" >
<meta name="tags" content="<? echo $hashtags ?>">

实际上我用这个脚本删除了"#"并输入了","

$hashclear = preg_replace('/#([A-Za-z0-9]+)/', '$1,', $post);

我怎样才能得到这些?有什么想法吗?

你的意思是这样的:

$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);
$hashTag = "#helloworld #test #video #youtube Bon Iver are the best daily surprise :D";
$str = str_replace('#', '', 
    preg_replace('/(?:#['w-]+'s*)+$/', '', $hashTag);
echo $str;