Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:22:35+00:00 2026-06-17T23:22:35+00:00

On a replacement alternative for inet_pton() in PHP, the following code is given: <?php

  • 0

On a replacement alternative for inet_pton() in PHP, the following code is given:

<?php
function inet_pton($ip)
{
    # ipv4
    if (strpos($ip, '.') !== FALSE) {
        $ip = pack('N',ip2long($ip));
    }
    # ipv6
    elseif (strpos($ip, ':') !== FALSE) {
        $ip = explode(':', $ip);
        $res = str_pad('', (4*(8-count($ip))), '0000', STR_PAD_LEFT);
        foreach ($ip as $seg) {
            $res .= str_pad($seg, 4, '0', STR_PAD_LEFT);
        }
        $ip = pack('H'.strlen($res), $res);
    }
    return $ip;
}
?>

But when testing this using the following test code, it shows that not all entries are correct:

<?php
$arrIPs = array(
        "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
        "fe80:01::af0",
        "::af0",
        "192.168.0.1",
        "0000:0000:0000:0000:0000:0000:192.168.0.1");
foreach($arrIPs as $strIP) {
    $strResult = bin2hex(inet_pton($strIP));
    echo "From: {$strIP} to: {$strResult}<br />\n";
}
/*
From: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 to: 20010db885a3000000008a2e03707334
From: fe80:01::af0 to: 0000000000000000fe80000100000af0 //Incorrect
From: ::af0 to: 00000000000000000000000000000af0
From: 192.168.0.1 to: c0a80001
From: 0000:0000:0000:0000:0000:0000:192.168.0.1 to: 00000000 //Incorrect
*/
?>

I don’t know about the correct IPv6 syntax, so I prefer if someone else, who knows more about IPv6 and standards, looks at this and tells me what’s wrong with it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:22:37+00:00Added an answer on June 17, 2026 at 11:22 pm

    This code will do it right:

    function inet_pton($ip){
        # ipv4
        if (strpos($ip, '.') !== FALSE) {
            if (strpos($ip, ':') === FALSE) $ip = pack('N',ip2long($ip));
            else {
                $ip = explode(':',$ip);
                $ip = pack('N',ip2long($ip[count($ip)-1]));
            }
        }
        # ipv6
        elseif (strpos($ip, ':') !== FALSE) {
            $ip = explode(':', $ip);
            $parts=8-count($ip);
            $res='';$replaced=0;
            foreach ($ip as $seg) {
                if ($seg!='') $res .= str_pad($seg, 4, '0', STR_PAD_LEFT);
                elseif ($replaced==0) {
                    for ($i=0;$i<=$parts;$i++) $res.='0000';
                    $replaced=1;
                } elseif ($replaced==1) $res.='0000';
            }
            $ip = pack('H'.strlen($res), $res);
        }
        return $ip;
    }
    

    Results:

    From: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 to:
    string '20010db885a3000000008a2e03707334' (length=32)
    
    From: fe80:01::af0 to:
    string 'fe800001000000000000000000000af0' (length=32)
    
    From: ::af0 to:
    string '00000000000000000000000000000af0' (length=32)
    
    From: 192.168.0.1 to:
    string 'c0a80001' (length=8)
    
    From: 0000:0000:0000:0000:0000:0000:192.168.0.1 to:
    string 'c0a80001' (length=8)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an alternative way to do the following one-liner replacement of text across
What is the C# replacement for the following define? #define IS_FINITE(x) (0x7FF0 != (*((unsigned
I'm building an alternative Home application (a replacement of the standard launcher). There's something
Since PHP's call_user_method() and call_user_method_array() are marked deprecated I'm wondering what alternative is recommended?
In the following code I use bootstrapping to calculate the C.I. and the p-value
I'm currently refactoring code to replace Convert.To's to TryParse. I've come across the following
I am writing a replacement Windows shell in C#, and the feature I am
Is there any replacement for saxon:if and saxon:before functions in XSLT 2.0 / XPath
In a replacement pattern, is there any way to print the NUMBER of the
I want to build a lock screen replacement application. Is there any way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.