Mac*_*Mac 79 php arrays 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!';
}
Run Code Online (Sandbox Code Playgroud)
因为在使用它时,它不起作用,如果有这样的东西会很好
Bin*_*min 121
@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);
}
Run Code Online (Sandbox Code Playgroud)
如何使用:
$string = 'Whis string contains word "cheese" and "tea".';
$array = array('burger', 'melon', 'cheese', 'milk');
if (strposa($string, $array, 1)) {
echo 'true';
} else {
echo 'false';
}
Run Code Online (Sandbox Code Playgroud)
将返回true
,因为array
"cheese"
.
更新:在找到第一个针时停止改进代码:
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
Run Code Online (Sandbox Code Playgroud)
Leo*_*eon 35
str_replace要快得多.
$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
$match = (str_replace($find_letters, '', $string) != $string);
Run Code Online (Sandbox Code Playgroud)
Dav*_*ave 17
下面的代码不仅展示了如何做到这一点,而且还将它放在一个易于使用的功能中.它是由"jesda"编写的.(我在网上找到了)
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;
}
?>
Run Code Online (Sandbox Code Playgroud)
用法:
$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
Run Code Online (Sandbox Code Playgroud)
问题是,提供的示例只是“示例”还是您要查找的内容?这里有许多混合的答案,我不明白接受的答案的复杂性。
要找出字符串中是否存在针头数组的任何内容,并快速返回 true 或 false:
$string = 'abcdefg';
if(str_replace(array('a', 'c', 'd'), '', $string) != $string){
echo 'at least one of the needles where found';
};
Run Code Online (Sandbox Code Playgroud)
要找出是否所有的值存在针的阵列的字符串中,因为在这种情况下,所有三个'a', 'b'
和'c'
必须存在,像你提到的你“例如”
echo '所有字母都在字符串中!';
这里的许多答案都与上下文无关,但我怀疑您标记为已解决的问题的意图。例如,接受的答案是一针
$array = array('burger', 'melon', 'cheese', 'milk');
Run Code Online (Sandbox Code Playgroud)
如果所有这些词都必须在字符串中找到怎么办?
然后你"not accepted answers"
在这个页面上尝试一些。
您可以遍历数组并在strpos
返回时设置"flag"值false
.
$flag = false;
foreach ($find_letters as $letter)
{
if (strpos($string, $letter) === false)
{
$flag = true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后检查值$flag
.
如果您只想检查字符串中是否存在某些字符,请使用strtok:
$string = 'abcdefg';
if (strtok($string, 'acd') === $string) {
// not found
} else {
// found
}
Run Code Online (Sandbox Code Playgroud)