当搜索一个字符串时,如何使用strpos
查找多个不同的子字符串?例如:
$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
if(strpos($string, $find_letters) !== false)
{
echo 'All the letters are found in the string!';
}
因为使用这个时,它不起作用,如果有类似这样的东西就好了。
@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);
}
如何使用:
$string = 'Whis string contains word "cheese" and "tea".';
$array = array('burger', 'melon', 'cheese', 'milk');
if (strposa($string, $array, 1)) {
echo 'true';
} else {
echo 'false';
}
因为array
包含"cheese"
,所以将返回true
。
更新:改进了代码以在找到第一个针时停止搜索:
function strposa(string $haystack, array $needles, int $offset = 0): bool
{
foreach($needles as $needle) {
if(strpos($haystack, $needle, $offset) !== false) {
return true; // stop on first true result
}
}
return false;
}
$string = 'This string contains word "cheese" and "tea".';
$array = ['burger', 'melon', 'cheese', 'milk'];
var_dump(strposa($string, $array)); // will return true, since "cheese" has been found
str_replace更快。
$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
$match = (str_replace($find_letters, '', $string) != $string);
<?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;
}
?>
使用方法:
$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
$string = 'abcdefg';
if(str_replace(array('a', 'c', 'd'), '', $string) != $string){
echo 'at least one of the needles where found';
};
许多答案都超出了这个背景,但我怀疑问题的意图是否已得到解决。例如,被接受的答案是一个针。echo 'All the letters are found in the string!';
$array = array('burger', 'melon', 'cheese', 'milk');
"不被接受的答案"
。strpos
返回 false
时设置一个“标志”值。$flag = false;
foreach ($find_letters as $letter)
{
if (strpos($string, $letter) !== false)
{
$flag = true;
}
}
然后检查$flag
的值。
!== false
而不是!== flase
。 - Joe Huang如果您只想检查某些字符是否实际存在于字符串中,请使用strtok:
$string = 'abcdefg';
if (strtok($string, 'acd') === $string) {
// not found
} else {
// found
}
这个表达式用于搜索所有字母:
count(array_filter(
array_map("strpos", array_fill(0, count($letters), $str), $letters),
"is_int")) == count($letters)
function in_array_strpos($word, $array){
foreach($array as $a){
if (strpos($word,$a) !== false) {
return true;
}
}
return false;
}
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;
}
我将这个与被接受的答案进行了基准测试,并且当使用一个超过7个$needles
的数组时,速度会显著地更快。
function strposar($arrayOfNeedles, $haystack){
if (count(array_filter($arrayOfNeedles, function($needle) use($haystack){
return strpos($haystack, $needle) !== false;
})) > 0){
return true;
} else {
return false;
}
}
strposar($arrayOfNeedles, $haystack); //returns true/false
if (count(array_filter($arrayOfNeedles, function($needle) use($haystack){
return strpos($haystack, $needle) !== false;
})) > 0){
//found so do this
} else {
//not found do this instead
}
strposa
将遍历整个文本,但这并不是必要的!我表达清楚了吗? - Arnaudforeach
将在数组“cheese”上停止,并且不会检查“milk”,因为已经找到了一个有效结果。我也尝试过使用if(!empty($res)) break;
的实现方式,但是发现它也可以正常工作。通过在foreach
中添加$res = strpos($haystack, $query, 1); if(!empty($res)) break;
来检查它。 - Binyaminforeach($needle as $k => $query) { if(strpos($haystack, $query, $offset) !== false) return $k; }
来改进它,以便返回匹配项的键以供进一步处理。 - James Cameron