Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    block the dot at the end

    PHP Code:
    $ip "10.0.8.101";
    $oct explode(".",$ip);

    for (
    $i=0$i<5$i++){
    echo 
    decbin($oct[$i]).".";

    how can i block the dot at the and of the output ? (i have marked it as red)

    Code:
    1010.0.1000.1100101.0.

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    Code:
    for ($i=0; $i<5; $i++){ 
         echo decbin($oct[$i]);
         if ($i < 5) {
              ".";
         }
    }
    Might have to change it to 4, i'm not sure. Probably a simpler method too.

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by flynch01 View Post
    Code:
    for ($i=0; $i<5; $i++){ 
         echo decbin($oct[$i]);
         if ($i < 5) {
              ".";
         }
    }
    Might have to change it to 4, i'm not sure. Probably a simpler method too.
    lower than 4 is 3 . ther is no with 3 octed IP right ?

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    The decimal is appearing at the end because you are concatenating a decimal within each iteration of the for-loop.

    Here's a much more staight-forward method, no need for a for-loop:
    PHP Code:
    // define the IP
    $ip '10.0.8.101';

    // convert the decimal values to binary
    $decbin explode'.'$ip );
    $decbin array_map'decbin'$decbin );
    $decbin implode'.'$decbin );

    // send it to the client
    echo $decbin
    Last edited by kbluhm; 03-12-2008 at 04:37 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •