【问题标题】:Search strpos() with a Number Range PHP使用数字范围 PHP 搜索 strpos()
【发布时间】:2018-01-26 11:31:09
【问题描述】:

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

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

请原谅我的英语不好。

【问题讨论】:

  • 使用正则表达式而不是 strpos。如果您需要更多帮助,请提供一个完整的最小示例。
  • @DanFarrell 我的目标是获取具有“tcp --dport 1-65355 -j DROP”的字符串。例如“tcp --dport 8080 -j DROP”或“tcp --dport 21 -j DROP”
  • 向我们展示您迄今为止的尝试和发生的情况,我们将帮助您找出解决方法。
  • @DanFarrell 我没有尝试太多,我使用了很多 strpos,但不是在这个级别,所以我真的不知道该尝试什么。我很高兴

标签: php iptables strpos


【解决方案1】:

假设每个字符串都像您的示例一样采用固定格式,您可以搜索数字,然后使用 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。当在字符串上找不到数字时将导致未定义的错误/警告
2。发现超过 1 个整数时可能会变得不准确

【讨论】:

  • 我怎样才能得到一个带数字的变量?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-12
  • 2011-06-20
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多