Get all system IPv4 addresses as array in PHP

/**
 * Get system ipv4 addresses.
 * Works on Linux only.
 * @return [array]
 */
function getSystemIPs() {
    $ips = shell_exec('hostname -I');
    $ips = preg_split('/\s+/', $ips, -1, PREG_SPLIT_NO_EMPTY);

    foreach ($ips as $ip) {
        if (filter_var($ip,
            FILTER_VALIDATE_IP,
            FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
            
            $ip_a[] = $ip;
        }
    }

    return $ip_a;
}
Tags