搜索模式频率
最好是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://www.php.net/manual/en/function.substr -count.php
http://www.php.net/manual/en/function.substr-count.php
您可以使用 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);
像这样的东西应该有效:
请注意 substr_count 区分大小写(这就是为什么我对上面代码中的所有内容进行 strtolower() 处理)。这可以很容易地删除,因此上面的代码也区分大小写。
Something like this should work:
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.