搜索模式频率

发布于 2024-12-20 02:24:45 字数 230 浏览 0 评论 0原文

最好是 PHP 解决方案——但任何想法都很棒。

给出一个文本 blob

'这是一个包含一些内容的超级字符串,我想找到红色毛衣和紫色大象。紫色大象会数两次。红色毛衣将计数 3 次,因为红色毛衣出现了 3 次”

,并且短语列表

“红色毛衣,紫色大象”

想要搜索文本 blob 并返回出现次数,

因此

红色毛衣 = 3 紫色大象 = 2

Preferably PHP solutions -- but any ideas would be great.

Give a text blob

'This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times'

and a phrase list

'red sweaters, purple elephants'

want to search the text blob and return count of occurances

therefore

red sweaters = 3
and purple elephants = 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

表情可笑 2024-12-27 02:24:45

http://www.php.net/manual/en/function.substr -count.php

$string = 'This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times';

$keys = 'red sweaters, purple elephants';

$pkeys = explode(', ', $keys);
foreach($pkeys as $key)
{
    printf("%s occourrences: %d\n", $key, substr_count($string, $key));
}

http://www.php.net/manual/en/function.substr-count.php

$string = 'This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times';

$keys = 'red sweaters, purple elephants';

$pkeys = explode(', ', $keys);
foreach($pkeys as $key)
{
    printf("%s occourrences: %d\n", $key, substr_count($string, $key));
}
菩提树下叶撕阳。 2024-12-27 02:24:45

您可以使用 substr_count 来搜索文本中的字符串。请注意,在您的示例中,如果文本是“棕色红色毛衣”,则“红色毛衣”将计为 +1。

您还可以使用正则表达式。类似于 preg_match("/$string/",$text);。这将返回找到该字符串的时间。

此外,如果您想搜索由逗号分隔的多个字符串(如您的示例),您首先需要拆分字符串。您可以使用 explode 来实现此目的。 $strings =explode(",",$search);

You can use substr_count which will search for strings inside a text. Just note that in your example if the text was "brownred sweaters" that will count +1 for "red sweaters".

You can also use regular expressions. Something like preg_match("/$string/",$text);. This would return the times the string was found.

Also if you want to search for several strings delimited by a comma (like your example) you first need to split the string. You can use explode for this. $strings = explode(",",$search);

相守太难 2024-12-27 02:24:45

像这样的东西应该有效:

<?php
  $string = strtolower('This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times');

  $allprases = 'red sweaters, purple elephants'

  $phrasearray = explode(',',$allphrases);

  foreach ($phrasearray as $k => $phrase) {
    $phrase = strtolower(trim($phrase));
    echo 'String '.$phrase.' found '.substr_count($string,$phrase).' times.<br />';
  }
?>

请注意 substr_count 区分大小写(这就是为什么我对上面代码中的所有内容进行 strtolower() 处理)。这可以很容易地删除,因此上面的代码也区分大小写。

Something like this should work:

<?php
  $string = strtolower('This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times');

  $allprases = 'red sweaters, purple elephants'

  $phrasearray = explode(',',$allphrases);

  foreach ($phrasearray as $k => $phrase) {
    $phrase = strtolower(trim($phrase));
    echo 'String '.$phrase.' found '.substr_count($string,$phrase).' times.<br />';
  }
?>

Do note that substr_count is case sensitive (which is why I'm strtolower()ing everything in the above code). This can be removed easily enough so that the code above is case sensitive too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文