使用数字范围 PHP 搜索 strpos()

Search strpos() with a Number Range PHP

我想在一个端口被阻止的字符串中搜索规则 iptables,并且我想显示该端口。

例如,规则 iptables -A OUTPUT -p tcp --dport 8080 -j DROP; 我希望能够通过 strpos 搜索字符串 tcp --dport 8080 -j DROP 但数字范围 1-65335 instead of 8080

请原谅我糟糕的英语。

假设每个字符串都像您的示例一样采用固定格式,您可以搜索数字,然后使用 preg_match() 函数

检查是否在范围内
$str = 'iptables -A OUTPUT -p tcp --dport 8080 -j DROP;';

preg_match('/\d+/', $str, $match); // search for matches
if ($match[0] >= 1 && $match[0] <= 65335) {
    echo "Valid number found on string";
}

模式/\d+/搜索任何数字,1到无限次

备注:
1。当在字符串上找不到数字时将导致未定义 errors/warnings
2。当找到超过 1 个整数时可能会变得不准确