【问题标题】:How to find tags From a string如何从字符串中查找标签
【发布时间】:2014-04-09 21:03:35
【问题描述】:

我正在写一个博客脚本。

我想从文章中找到标签..

在 php 中从正文字符串中查找标签的最佳方法是什么

示例: 如果文章正文包含此字符串

'这个问题是关于 php 字符串的'

所以脚本应该得到 php stringQuestion 作为标签。

【问题讨论】:

  • explode 按空格并对照列表检查每个单词?
  • 你的意思是我必须使用单词列表?
  • 查看DOM Parser
  • 脚本如何知道哪些词是标签?在您的示例中,为什么 thisisabout 不被视为标签?您不认为它们标记的唯一原因是在您的脑海中,您有一个您认为常见的单词列表。因此,您要么需要创建一个常用词列表并排除这些词,要么创建一个不常用词列表并匹配以考虑所有标签。
  • 另外,不要忘记单词的变化。 string 这个词也会被标记吗?你有strings。那么你会创建一个单词列表和所有可能的变体吗?你可以。您还可以创建一个语法词法分析器,使用适当的语法来查找单词的变体。

标签: php string tags blogs


【解决方案1】:

有很多方法,但其中一种可能是使用strpos

$mystring = 'This Question is about php strings';

$wordsToFind = array('php','strings','Question');

foreach ($wordsToFind as $word)
    if (strpos($mystring, $word)) {
        echo $word.' has been found'; echo "\n";
    }

您可以try it out here

【讨论】:

  • stripos 可能会更好,因此不考虑大小写。
【解决方案2】:

您必须先定义标签。然后脚本会找到文章中存在的任何一个,并将标签添加到另一个 array() 中,这将是该特定文章的标签。

$defined_tags = array('php', 'string', 'questions', 'javascript', 'python');
$article_tags = array();
$string = "This Question is about php strings";

foreach($defined_tags as $tag)
{
    if (strpos($string,$tag) !== false) 
    {
        $article_tags[] = $tag;    
    }
}

【讨论】:

  • 好.. 我正在写一个 sn-p 来回答我自己的问题.. 但是做到了.. 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2019-12-04
  • 1970-01-01
  • 2011-01-29
  • 2021-07-16
  • 1970-01-01
  • 2021-07-02
相关资源
最近更新 更多