0

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;
}
Naomi
  • 97
  • 5

3 Answers3

0

the first parameter of ereg is regular expression. So, .*pointer match anything (.*), then the word "pointer" (pointer), then the rest of expression.

barbacan
  • 632
  • 6
  • 16
0

Not much to it really. All you need to do is add a marker character to the start and end of the regex string.

Typically a marker character would be a slash (/), but it can be others (tilde ~ is used quite commonly and would work well for you here), as long as it's the same character at the start and end of the string, and doesn't appear within the string (you'd need to escape it with a backslash if it does).

So your code could look like this:

preg_match('~.*pointer ([A-Za-z0-9.-]+)\..*~', $output, $regs)

Note, if you use a slash as your regex marker character, you will need to double-it up, as slash is also an escape character in a PHP string.

In terms of explaining the actual expression:

.* - this is any number of any characters at the start of the string (you could actually leave this off this expression; it won't affect the matching)

pointer - this is looking for the actual word 'pointer' in the string being matched.

([A-Za-z0-9.-]+) - looks for one or more characters which are alpha-numeric or dot or hyphen. In addition, because these are enclosed in brackets, they become a 'matching group', which means that the result of this part of the search ends up in $regs[1].

\..* - looks for a dot character, followed by any number of any characters. As with the begining of the match, the .* can be dropped as it won't affect the matching.

So the whole expression is looking for a string which looks something like this:

blahblahblahpointer blah123-.blah.blahblahblah

and from that, you will get blah123-.blah in $regs[1].

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

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;
}
meouw
  • 41,754
  • 10
  • 52
  • 69