PHP 使用 version_compare () 函式判斷含符號數值是否在某一區間內

yichengo發表於2018-12-12

之前拿ip舉的例子可能不太恰當,修改一下。

/**
 * 判斷變數是否在範圍內
 *
 * @param string $strnum 1.1.1.1
 * @param string $min
 * @param string $max
 * @return bool
 */
function number_segment_between($strnum, $min, $max)
{
    return version_compare($strnum, $min, '>=') and version_compare($strnum, $max, '<=');
}

/**
 * 測試使用方法,為了輸出結果更直觀
 *
 * @param string $strnum 1.1.1.1
 */
function test_print($strnum, $min, $max)
{
    $format = "number_segment_between('%s', '{$min}', '{$max}') : ";
    print_r(sprintf($format, $strnum) . (number_segment_between($strnum, $min, $max) ? 'True' : 'False') . "\n");
}

echo '<pre>';
$min = '100.0.100.5';
$max = '100.0.100.20';
echo "測試.分隔比較:\n";
test_print('127.0.0.1', $min, $max);
test_print('1.1.1.1', $min, $max);
test_print('255.0.0.1', $min, $max);
test_print('100.0.100.5', $min, $max);
test_print('100.0.99.5', $min, $max);
test_print('200.0.100.100', $min, $max);
test_print('200.0.101.100', $min, $max);
test_print('100.0.1.10', $min, $max);
test_print('100.0.100.10', $min, $max);
echo "\n\n";

$min = '100-0-100-5-1-1';
$max = '100-0-100-20-1-1';
echo "測試-分隔比較:\n";
test_print('127-0-0-1-1-1', $min, $max);
test_print('1-1-1-1-1-1', $min, $max);
test_print('255-0-0-1-1-1', $min, $max);
test_print('100-0-100-5-1-1', $min, $max);
echo "\n\n";

$min = '100_0_100_5_0_1_0_1';
$max = '100_0_100_20_0_1_0_1';
echo "測試_分隔比較:\n";
test_print('127_0_0_1_0_1_0_1', $min, $max);
test_print('1_1_1_1_0_1_0_1', $min, $max);
test_print('255_0_0_1_0_1_0_1', $min, $max);
test_print('100_0_100_5_0_1_0_1', $min, $max);

測試.分隔比較結果輸出:

number_segment_between('127.0.0.1', '100.0.100.5', '100.0.100.20') : False
number_segment_between('1.1.1.1', '100.0.100.5', '100.0.100.20') : False
number_segment_between('255.0.0.1', '100.0.100.5', '100.0.100.20') : False
number_segment_between('100.0.100.5', '100.0.100.5', '100.0.100.20') : True
number_segment_between('100.0.99.5', '100.0.100.5', '100.0.100.20') : False
number_segment_between('200.0.100.100', '100.0.100.5', '100.0.100.20') : False
number_segment_between('200.0.101.100', '100.0.100.5', '100.0.100.20') : False
number_segment_between('100.0.1.10', '100.0.100.5', '100.0.100.20') : False
number_segment_between('100.0.100.10', '100.0.100.5', '100.0.100.20') : True

測試-分隔比較結果輸出

number_segment_between('127-0-0-1-1-1', '100-0-100-5-1-1', '100-0-100-20-1-1') : False
number_segment_between('1-1-1-1-1-1', '100-0-100-5-1-1', '100-0-100-20-1-1') : False
number_segment_between('255-0-0-1-1-1', '100-0-100-5-1-1', '100-0-100-20-1-1') : False
number_segment_between('100-0-100-5-1-1', '100-0-100-5-1-1', '100-0-100-20-1-1') : True

測試_分隔比較結果輸出

number_segment_between('127_0_0_1_0_1_0_1', '100_0_100_5_0_1_0_1', '100_0_100_20_0_1_0_1') : False
number_segment_between('1_1_1_1_0_1_0_1', '100_0_100_5_0_1_0_1', '100_0_100_20_0_1_0_1') : False
number_segment_between('255_0_0_1_0_1_0_1', '100_0_100_5_0_1_0_1', '100_0_100_20_0_1_0_1') : False
number_segment_between('100_0_100_5_0_1_0_1', '100_0_100_5_0_1_0_1', '100_0_100_20_0_1_0_1') : True

version_compare()方法介紹

  • version_compare — 對比兩個「PHP 規範化」的版本數字字串
  • 手冊地址
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章