-3

I had this original code:

/**
* IP address conversion: A.B.C.D -> log
*
* @param string $string IP Address: A.B.C.D
* @return long
* @access private
* @static
* @since 1.0
*/
private function convert2number($string)
{
    $pattern= "\"([0-9]+)\"";
    if (ereg($pattern, $string, $regs))
        return (int)$regs[1];
}

/**
* IP conversion
*
* @param string $address IP address in the A.B.C.D format
* @return long
* @access private
* @static
* @since 1.0
*/
private function IpAddress2IpNumber($address)
{
    $pattern = "([0-9]+).([0-9]+).([0-9]+).([0-9]+)";

    if (ereg($pattern, $address, $regs))
        return $number = $regs[1] * 256 * 256 * 256 + $regs[2] * 256 * 256 + $regs[3] * 256 + $regs[4];
    else
        return false;
}

Since PHP 5.3, ereg was Deprecated, I researched this, and I stumbled upon a suggestion to replace ereg with preg_match, I did so, and I got a new error:

Warning: preg_match(): Unknown modifier '.' in geoip.php on line 222

Code:

/**
* IP conversion
*
* @param string $address IP address in the A.B.C.D format
* @return long
* @access private
* @static
* @since 1.0
*/
private function IpAddress2IpNumber($address)
{
    $pattern = "([0-9]+).([0-9]+).([0-9]+).([0-9]+)";

    if (preg_match($pattern, $address, $regs))
        return $number = $regs[1] * 256 * 256 * 256 + $regs[2] * 256 * 256 + $regs[3] * 256 + $regs[4];
    else
        return false;
}

How do I go about fixing this? Any recommendation? Thanks!

Makyen
  • 31,849
  • 12
  • 86
  • 121
Sup3r
  • 3
  • 5

2 Answers2

1

You need to use delimiters with preg_match. See the docs on this topic.

elixenide
  • 44,308
  • 16
  • 74
  • 100
-1
  1. "." needs a "\" to avoid it be taken as any character.
  2. the pattern needs delimiters for preg_match.

    $pattern = "|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)|";
    
harrrrrrry
  • 13,643
  • 2
  • 23
  • 28
  • Don't just give away code. Explain why there is an issue and describe how to fix it. Like the man and his fishing rod – Cjmarkham Jan 19 '15 at 22:24
  • Thanks @zairwolf, What's the correct way? `$pattern = "/([0-9]+).([0-9]+).([0-9]+).([0-9]+)/"; ` OR `$pattern = "|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)|";` – Sup3r Jan 19 '15 at 22:28
  • A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.. Read this : http://php.net/manual/en/regexp.reference.delimiters.php – harrrrrrry Jan 19 '15 at 22:30