strpos() 有多针?
我正在寻找像 strpos() 这样的函数,它有两个显着的区别:
- 能够接受多个针。我的意思是数千针。
- 搜索大海捞针中所有出现的针并返回起始位置数组。
当然,它必须是一个有效的解决方案,而不仅仅是穿过每根针的一个循环。我搜索过这个论坛,发现了与此类似的问题,例如:
但它们都不是我要寻找的。我使用 strpos 只是为了更好地说明我的问题,可能必须使用完全不同的东西来达到此目的。
我知道 Zend_Search_Lucene 并且我很感兴趣是否可以用于实现此目的以及如何实现(只是总体思路)?
非常感谢您的帮助和时间!
I am looking for a function like strpos() with two significant differences:
- To be able to accept multiple needles. I mean thousands of needles at ones.
- To search for all occurrences of the needles in the haystack and to return an array of starting positions.
Of course it has to be an efficient solution not just a loop through every needle. I have searched through this forum and there were similar questions to this one, like:
- Using an array as needles in strpos
- Define multiple needles using stripos
- Can't search an array in PHP in_array for the presence of multiple needles
but nether of them was what I am looking for. I am using strpos just to illustrate my question better, probably something entirely different has to be used for this purpose.
I am aware of Zend_Search_Lucene and I am interested if it can be used to achieve this and how (just the general idea)?
Thanks a lot for Your help and time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
preg 匹配
尝试对多个检查多个 strpos 值进行
try preg match for multiple
Checking for multiple strpos values
以下是我的策略的一些示例代码:
我在上面实现了一个简单的蛮力方法,该方法适用于针和大海捞针的任何组合(不仅仅是单词)。对于可能更快的算法,请查看:
其他解决方案
Here's some sample code for my strategy:
I've implemented a simple brute force method above that will work with any combination of needles and haystacks (not just words). For possibly faster algorithms check out:
Other Solution
我知道这并不能回答OP的问题,但想发表评论,因为此页面位于Google顶部的多针strpos。这是一个简单的解决方案(同样,这不是特定于操作员的问题 - 抱歉):
如果将 2 个项目添加到 $missing 数组中,则意味着输入不满足 $ 中的任何图像格式img_formats 数组。那时你知道你可以返回一个错误等。这可以很容易地变成一个小函数:
回到我们的第一个例子,然后使用该函数:
当然,你在函数返回 true 或 false 后做什么是 up给你。
I know this doesn't answer the OP's question but wanted to comment since this page is at the top of Google for strpos with multiple needles. Here's a simple solution to do so (again, this isn't specific to the OP's question - sorry):
If 2 items are added to the $missing array that means that the input doesn't satisfy any of the image formats in the $img_formats array. At that point you know that you can return an error, etc. This could easily be turned into a little function:
Back to our first example using then the function instead:
Of course, what you do after the function returns true or false is up to you.
您似乎正在搜索整个单词。在这种情况下,类似这样的事情可能会有所帮助。由于它使用内置函数,它应该比自定义代码更快,但您必须对其进行分析:
以正确的格式存储信息(如针)将改善运行时间(例如,因为您不必调用 <代码>array_flip)。
str_word_count
文档中的注释:因此,请确保您设置了正确的区域设置。
It seems you are searching for whole words. In this case, something like this might help. As it uses built-in functions, it should be faster than custom code, but you have to profile it:
Storing the information (like the needles) in the right format will improve the runtime ( e.g. as you don't have to call
array_flip
).Note from the
str_word_count
documentation:So make sure you set the locale right.
您可以使用正则表达式,它们支持 OR 运算。然而,与 strpos 相比,这会使其相当慢。
You could use a regular expression, they support OR operations. This would however make it fairly slow, compared to strpos.
使用
array_map()
的简单解决方案怎么样?作为返回,您将拥有一个数组,其中键是针位置,值是起始位置(如果找到)。
How about a simple solution using
array_map()
?As return, you're going to have an array where the keys are the needles positions and the values are the starting positions, if found.