[英]Checking for multiple strpos values
我想知道如何完成多個 strpos 檢查。
讓我澄清一下:
我希望 strpos 檢查變量“COLOR”以查看變量中是否有從 1 到 8 的任何數字。 如果存在從 1 到 8 的任何數字,我想回顯“已選擇”。
例子:
假設變量中只有數字 1,它將回顯“已選擇”。
假設數字 1 2 和 3 在變量中,它將回顯“已選擇”。
假設數字 3 9 25 在變量中,它將回顯“selected”(因為 3.!)。
假設變量中只有數字 9,它不會回顯。
假設數字 9 25 48 在變量中,它不會回顯。
我剛剛使用了 OR 語句 (||)
<?php
if (strpos($color,'1') || strpos($color,'2') || strpos($color,'3') || strpos($color,'4') || strpos($color,'5') || strpos($color,'6') || strpos($color,'7') || strpos($color,'8') === true)
{
//do nothing
} else {
echo "checked";
}
?>
我發現上述答案不完整,並提出了我自己的功能:
/**
* Multi string position detection. Returns the first position of $check found in
* $str or an associative array of all found positions if $getResults is enabled.
*
* Always returns boolean false if no matches are found.
*
* @param string $str The string to search
* @param string|array $check String literal / array of strings to check
* @param boolean $getResults Return associative array of positions?
* @return boolean|int|array False if no matches, int|array otherwise
*/
function multi_strpos($string, $check, $getResults = false)
{
$result = array();
$check = (array) $check;
foreach ($check as $s)
{
$pos = strpos($string, $s);
if ($pos !== false)
{
if ($getResults)
{
$result[$s] = $pos;
}
else
{
return $pos;
}
}
}
return empty($result) ? false : $result;
}
用法:
$string = "A dog walks down the street with a mouse";
$check = 'dog';
$checks = ['dog', 'cat', 'mouse'];
#
# Quick first position found with single/multiple check
#
if (false !== $pos = multi_strpos($string, $check))
{
echo "$check was found at position $pos<br>";
}
if (false !== $pos = multi_strpos($string, $checks))
{
echo "A match was found at position $pos<br>";
}
#
# Multiple position found check
#
if (is_array($found = multi_strpos($string, $checks, true)))
{
foreach ($found as $s => $pos)
{
echo "$s was found at position $pos<br>";
}
}
如果所有值都由值中的空格分隔,那么您可以執行以下操作。 否則忽略它。
這是必要的,因為如果你有$color="25";
然后strpos
將同時找到 2、5 和 25,因此不會出現所需的結果
<?php
$color='1 25 48 9 3';
$color_array = explode(" ",$color);
$find = range(1,8);//array containing 1 to 8
$isFound = false;
foreach($find as $value) {
if(in_array($value, $color_array))
{
$isFound = true;
break;
}
}
if($isFound) {
echo "Selected";
}
?>
if (preg_match('/string1|string2|string3/i', $str)){
//if one of them found
}else{
//all of them can not found
}
使用圍繞數字字符類的字邊界的簡單preg_match()
調用將完全准確並適合您的任務。
單詞邊界元字符確保執行全整數匹配——不會發生誤報(部分)匹配。
代碼:(演示)
$array = array(
'text 1 2 and 3 text',
'text 3 9 25 text',
'text 9 25 48 text',
);
foreach ($array as $color) {
echo "\n---\n$color";
echo "\n\t" , preg_match('~\b[1-8]\b~', $color, $out) ? "checked (satisfied by {$out[0]})" : 'not found';
echo "\n\tChad says: " , (strpos($color,'1') || strpos($color,'2') || strpos($color,'3') || strpos($color,'4') || strpos($color,'5') || strpos($color,'6') || strpos($color,'7') || strpos($color,'8') ? 'found' : 'not found');
}
輸出:
---
text 1 2 and 3 text
checked (satisfied by 1)
Chad says: found
---
text 3 9 25 text
checked (satisfied by 3)
Chad says: found
---
text 9 25 48 text
not found
Chad says: found
至於如何在你的腳本中實現這種技術......
if (!preg_match('~\b[1-8]\b~', $color)) {
echo 'checked';
}
我有類似的需求,所以這里是獲取給定字符串中最近子字符串位置的函數,其中搜索子字符串在數組中提供。 它還通過引用匹配的子字符串傳遞。 請注意,如果某些子字符串包含其他子字符串,則順序很重要 - 例如:'...' 和 '.'。
function strpos_arr($haystack, $needleN, $offset = 0, &$needle = '') {
if (!is_array($needleN)) {
return strpos($haystack, $needleN, $offset);
} else {
$res = FALSE;
foreach ($needleN as $ind => $item) {
$pos = strpos($haystack, $item, $offset);
if ($pos !== FALSE && ($res === FALSE || $pos < $res)) {
$res = $pos;
$needle = $item;
}
}
return $res;
}
}
請試試這個,它會幫助你。
if ( (strpos($str, $your_string) !== false) || (strpos($your_string, $str) !== false) ) {
// Matched
} else {
// Not Matched
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.