检查 php 中的重叠偏移量
如何区分字符串中具有重叠偏移量的单词。 例如,我有一个字符串:
$s = "The famous team violin will showcase there violin talent to the fans who love violin.";
然后我有一个关键字提取,它通过偏移量检索这些单词:
team violin - 11
violin - 16
基于结果单词“violin”与“team violion”重叠的结果,
正确的结果应该是:
team violin - 11
violin - 43
这是用于检索的代码每个关键字的偏移量
foreach($keywordArr as $keyword)
{
preg_match('/\b'.$keyword.'\b/',$s,$match,PREG_OFFSET_CAPTURE)
$keywordwithOffset = $keyword."\t".$match[0][1];
}
有人得到了一段可以解决这样的问题的php脚本吗?
How can i differentiate words in a string with overlapping offset.
For example i have a string :
$s = "The famous team violin will showcase there violin talent to the fans who love violin.";
and then i have a keyword extraction which retrieves these words with offset :
team violin - 11
violin - 16
base on the result above the resulting word "violin" overlaps with "team violion"
the right result should be :
team violin - 11
violin - 43
this is the code for retrieving the offset of each keyword
foreach($keywordArr as $keyword)
{
preg_match('/\b'.$keyword.'\b/',$s,$match,PREG_OFFSET_CAPTURE)
$keywordwithOffset = $keyword."\t".$match[0][1];
}
anyone got a piece of php script that could solve such thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您首先使用较大的关键字提取字符串并检索偏移量,然后用特殊字符(#,$等)替换大关键字,只要需要,字符串的长度就不会改变,所以当您正在搜索较小关键字的偏移量,它不会与较大关键字重叠。
I suggest you to extract the string first with the larger keyword and retrieve the offset, then replace the large keyword with special chars (#, $, etc.) as many as is needed that length of the string is not gonna change, so when you are searching for the offset of the smaller keyword it won't overlap the larger one.