检查 php 中的重叠偏移量

发布于 2024-11-29 02:25:44 字数 600 浏览 1 评论 0原文

如何区分字符串中具有重叠偏移量的单词。 例如,我有一个字符串:

$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 技术交流群。

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

发布评论

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

评论(1

第七度阳光i 2024-12-06 02:25:44

我建议您首先使用较大的关键字提取字符串并检索偏移量,然后用特殊字符(#,$等)替换大关键字,只要需要,字符串的长度就不会改变,所以当您正在搜索较小关键字的偏移量,它不会与较大关键字重叠。

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.

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