Search code examples
phpereg

How to convert this deprecated ereg() using pointer to preg_match()


Using php 5.3 - ereg() deprecated...

I'm trying to convert this function (to preg_match), but I don't understand the "pointer"...

function gethostbyaddr_new($ip)
{
    $output = `host -W 1 $ip`;

    if (ereg('.*pointer ([A-Za-z0-9.-]+)\..*', $output, $regs))
    {
        return $regs[1];
    }

    return $ip;
}

Solution

  • pointer is just a bit of text to be matched

    when I run host -W 1 I get
    4.4.8.8.in-addr.arpa domain name pointer google-public-dns-b.google.com.

    So you can use:

    function gethostbyaddr_new($ip)
    {
        $output = `host -W 1 $ip`;
    
        if (preg_match('/.*pointer ([A-Za-z0-9.-]+)\..*/', $output, $regs))
        {
            return $regs[1];
        }
    
        return $ip;
    }