在 strpos 中使用数组作为针
在搜索字符串时,如何将 strpos
用于针数组?例如:
$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
if(strpos($string, $find_letters) !== false)
{
echo 'All the letters are found in the string!';
}
因为用这个的时候不行,如果有这样的就好了
How do you use the strpos
for an array of needles when searching a string? For example:
$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
if(strpos($string, $find_letters) !== false)
{
echo 'All the letters are found in the string!';
}
Because when using this, it wouldn't work, it would be good if there was something like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
@Dave 来自 http://www.php.net/manual 的更新片段/en/function.strpos.php#107351
使用方法:
将返回
true
,因为array
"cheese"
。更新:改进了代码,在找到第一根针时停止:
@Dave an updated snippet from http://www.php.net/manual/en/function.strpos.php#107351
How to use:
will return
true
, because ofarray
"cheese"
.Update: Improved code with stop when the first of the needles is found:
str_replace 速度要快得多。
str_replace is considerably faster.
下面的代码不仅展示了如何做到这一点,而且还将其放入一个易于使用的函数中。它的作者是“杰斯达”。 (我在网上找到的)
PHP代码:
用法:
The below code not only shows how to do it, but also puts it in an easy to use function moving forward. It was written by "jesda". (I found it online)
PHP Code:
Usage:
问题是,提供的示例只是一个“示例”还是您正在寻找的内容?这里有很多混合的答案,我不明白公认的答案的复杂性。
要查明字符串中是否存在针数组的任何内容,并快速返回 true 或 false:
如果是,请给出@Leon 功劳。
找出字符串中是否存在针数组的所有值,在本例中,所有三个
'a'、'b'
和'c'
必须存在,就像您提到的作为您的“例如”这里的许多答案都脱离了这种背景,但我怀疑您标记为已解决的问题的意图。例如,接受的答案是
如果所有这些单词必须在字符串中找到怎么办?
然后您在此页面上尝试一些
“未接受的答案”
。The question, is the provided example just an "example" or exact what you looking for? There are many mixed answers here, and I dont understand the complexibility of the accepted one.
To find out if ANY content of the array of needles exists in the string, and quickly return true or false:
If, so, please give @Leon credit for that.
To find out if ALL values of the array of needles exists in the string, as in this case, all three
'a', 'b'
and'c'
MUST be present, like you mention as your "for example"Many answers here is out of that context, but I doubt that the intension of the question as you marked as resolved. E.g. The accepted answer is a needle of
What if all those words MUST be found in the string?
Then you try out some
"not accepted answers"
on this page.如果
strpos
返回false
,您可以迭代该数组并设置一个“标志”值。然后检查
$flag
的值。You can iterate through the array and set a "flag" value if
strpos
returnsfalse
.Then check the value of
$flag
.如果您只想检查字符串中是否确实存在某些字符,请使用 strtok:
If you just want to check if certain characters are actually in the string or not, use strtok:
此表达式搜索所有字母:
This expression searches for all letters:
你可以试试这个:
You can try this:
您还可以尝试使用 strpbrk() 进行否定(没有字母已找到):
You can also try using strpbrk() for the negation (none of the letters have been found):
这是我的方法。迭代字符串中的字符,直到找到匹配项。在更大的针头阵列上,这将优于可接受的答案,因为它不需要检查每根针头来确定是否已找到匹配项。
我将其与已接受的答案进行了基准测试,并且对于超过 7 个
$needles
的数组,这显着更快。This is my approach. Iterate over characters in the string until a match is found. On a larger array of needles this will outperform the accepted answer because it doesn't need to check every needle to determine that a match has been found.
I benchmarked this against the accepted answer and with an array of more than 7
$needles
this was dramatically faster.如果我只是想找出大海捞针中是否存在任何针,我使用
可重用函数
或 lambda 函数
If i just want to find out if any of the needles exist in the haystack, i use
reusable function
or lambda function
使用以下代码:
然后检查
$flag
的值。如果为true
,则已找到所有字母。如果不是,则为false
。With the following code:
Then check the value of
$flag
. If it istrue
, all letters have been found. If not, it'sfalse
.我正在写一个新答案,希望可以帮助任何寻找与我类似的人。
这适用于“我有多个针,我试图用它们来找到一个单独的字符串”的情况。这就是我遇到的问题。
数组
$found
将包含所有匹配针的列表,数组中具有最高计数值的项目将是您要查找的字符串,您可以通过以下方式获取:I'm writing a new answer which hopefully helps anyone looking for similar to what I am.
This works in the case of "I have multiple needles and I'm trying to use them to find a singled-out string". and this is the question I came across to find that.
The array
$found
will contain a list of all the matching needles, the item of the array with the highest count value will be the string(s) you're looking for, you can get this with:回复@binyamin和@Timo..(没有足够的点来添加评论..)但结果不包含位置..
下面的代码将返回第一个元素的实际位置,这就是 strpos 的目的。如果您希望找到 1 个匹配项,这很有用。如果您希望找到多个匹配项,则第一个找到的位置可能毫无意义。
Reply to @binyamin and @Timo.. (not enough points to add a comment..) but the result doesn't contain the position..
The code below will return the actual position of the first element which is what strpos is intended to do. This is useful if you're expecting to find exactly 1 match.. If you're expecting to find multiple matches, then position of first found may be meaningless.
只是上述答案的升级
Just an upgrade from above answers
我用这种代码来检查 hello,它还有编号功能。
如果您想在需要用户键入的网站中进行内容审核实践,则可以使用此选项
I used this kind of code to check for hello, It also Has a numbering feature.
You can use this if you want to do content moderation practices in websites that need the user to type