php 在 strpos 中使用数组作为针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6284553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using an array as needles in strpos
提问by MacMac
How do you use the strpos
for an array of needles when searching a string? For example:
strpos
搜索字符串时如何使用针头数组?例如:
$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
因为用这个的时候不行,要是有这种东西就好了
回答by Binyamin
@Dave an updated snippet from http://www.php.net/manual/en/function.strpos.php#107351
@Dave 来自http://www.php.net/manual/en/function.strpos.php#107351的更新片段
function strposa($haystack, $needles=array(), $offset=0) {
$chr = array();
foreach($needles as $needle) {
$res = strpos($haystack, $needle, $offset);
if ($res !== false) $chr[$needle] = $res;
}
if(empty($chr)) return false;
return min($chr);
}
How to use:
如何使用:
$string = 'Whis string contains word "cheese" and "tea".';
$array = array('burger', 'melon', 'cheese', 'milk');
if (strposa($string, $array, 1)) {
echo 'true';
} else {
echo 'false';
}
will return true
, because of array
"cheese"
.
会回来true
,因为array
"cheese"
。
Update:Improved code with stop when the first of the needles is found:
更新:找到第一个针时停止改进代码:
function strposa($haystack, $needle, $offset=0) {
if(!is_array($needle)) $needle = array($needle);
foreach($needle as $query) {
if(strpos($haystack, $query, $offset) !== false) return true; // stop on first true result
}
return false;
}
$string = 'Whis string contains word "cheese" and "tea".';
$array = array('burger', 'melon', 'cheese', 'milk');
var_dump(strposa($string, $array)); // will return true, since "cheese" has been found
回答by Leon
str_replace is considerably faster.
str_replace 的速度要快得多。
$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
$match = (str_replace($find_letters, '', $string) != $string);
回答by Dave
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)
下面的代码不仅展示了如何做到这一点,而且还把它放在一个易于使用的函数中。它是由“jesda”编写的。(我在网上找的)
PHP Code:
PHP代码:
<?php
/* strpos that takes an array of values to match against a string
* note the stupid argument order (to match strpos)
*/
function strpos_arr($haystack, $needle) {
if(!is_array($needle)) $needle = array($needle);
foreach($needle as $what) {
if(($pos = strpos($haystack, $what))!==false) return $pos;
}
return false;
}
?>
Usage:
用法:
$needle = array('something','nothing');
$haystack = "This is something";
echo strpos_arr($haystack, $needle); // Will echo True
$haystack = "This isn't anything";
echo strpos_arr($haystack, $needle); // Will echo False
回答by Evan Mulawski
You can iterate through the array and set a "flag" value if strpos
returns false
.
您可以遍历数组并在strpos
返回时设置“标志”值false
。
$flag = false;
foreach ($find_letters as $letter)
{
if (strpos($string, $letter) === false)
{
$flag = true;
}
}
Then check the value of $flag
.
然后检查 的值$flag
。
回答by netcoder
回答by mario
This expression searches for all letters:
此表达式搜索所有字母:
count(array_filter(
array_map("strpos", array_fill(0, count($letters), $str), $letters),
"is_int")) == count($letters)
回答by Jonas Lundman
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 ANYcontent of the array of needlesexists in the string, and quicklyreturn true or false:
要找出字符串中是否存在针头数组的任何内容,并快速返回 true 或 false:
$string = 'abcdefg';
if(str_replace(array('a', 'c', 'd'), '', $string) != $string){
echo 'at least one of the needles where found';
};
If, so, please give @Leoncredit for that.
To find out if ALLvalues of the array of needles existsin the string, as in this case, all three 'a', 'b'
and 'c'
MUST be present, like you mention as your "for example"
要找出是否所有的值存在针的阵列的字符串中,因为在这种情况下,所有三个'a', 'b'
和'c'
必须存在,像你提到的你“例如”
echo 'All the letters are found in the string!';
echo '所有字母都在字符串中!';
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
这里的许多答案都与上下文无关,但我怀疑您标记为已解决的问题的意图。例如,接受的答案是一针
$array = array('burger', 'melon', 'cheese', 'milk');
What if all those wordsMUST be found in the string?
如果所有这些词都必须在字符串中找到怎么办?
Then you try out some "not accepted answers"
on this page.
然后你"not accepted answers"
在这个页面上尝试一些。
回答by Rajnesh Nadan
You can try this:
你可以试试这个:
function in_array_strpos($word, $array){
foreach($array as $a){
if (strpos($word,$a) !== false) {
return true;
}
}
return false;
}
回答by billynoah
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.
这是我的方法。迭代字符串中的字符,直到找到匹配项。在更大的针头阵列上,这将优于公认的答案,因为它不需要检查每根针头以确定已找到匹配项。
function strpos_array($haystack, $needles = [], $offset = 0) {
for ($i = $offset, $len = strlen($haystack); $i < $len; $i++){
if (in_array($haystack[$i],$needles)) {
return $i;
}
}
return false;
}
I benchmarked this against the accepted answer and with an array of more than 7 $needles
this was dramaticallyfaster.
我这个基准对接受的答案,并与超过7数组$needles
,这是明显更快。