php - 如何拯救php中的两个分隔符?
问题描述
你好,我想将 ip 保存在一个变量中,我是通过控制台 ping 来实现的。
并标记我想要 [ 和 ] 中的内容。
$host = 'google.es';
function sacarIP($host)
{
exec("ping -n 1 $host", $output, $status);
$ip = $output[1];
return $ip;
}
echo sacarIP($host);
这让我回到:
Haciendo ping a google.es [142.250.184.3] con 32 bytes de datos:
解决方案
尝试:
$host = 'google.es';
function sacarIP($host)
{
exec("ping -n 1 $host", $output, $status);
preg_match('/\[\K[\d\.]+/', $output[1], $match);
$ip = $match[0];
return $ip;
}
echo sacarIP($host);
推荐阅读
- python - 模块级夹具未运行
- reporting-services - 在 SSRS 折线图中隐藏空零数据点
- dictionary - 在 Go 的非指针类型函数中初始化映射
- javascript - 如何收集laravel控制器传递的数组并通过javascript检索它
- javascript - 如何使用 React 过滤表?
- c# - CS1061 - 不包含“Id”的定义,并且没有可访问的扩展方法“Id”接受类型的第一个参数
- python - Python matplotlib subplot datetime x ticks cutomization
- react-native - React Native 更新 0.63.0 破坏了真 iPhone 上的登录功能
- c# - 仅当 C# 中未发生另一个异常时如何捕获异常
- javascript - 在请求中运行请求(Node & Cheerio)