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
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts

    finding second to last in a string?

    i am wanting to use just the first two numbers of the IP address.

    how do i find the second '.' in a string?

    thanks

    i.e.

    '192.168.0.1'

    and have returned...

    '192.168'

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $oldip '192.168.0.1';
    $newip preg_replace("/^(\d{1-3}\.\d{1-3})\.\d{1-3}\.\d{1-3}$/","$1",$oldip); 
    not tested.

    or use strpos, with limit and extract first part.

    best regards

  • #3
    New Coder
    Join Date
    Nov 2007
    Location
    Thane-Vashi,Mumbai,India.
    Posts
    44
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Simple method is to use explode() functions
    PHP Code:
    <?php
    $ipAddress
    ="192.168.0.1";
    $array=explode(".",$ipAddress);
    echo 
    "$array[0].$array[1]";//will return 192.168
    ?>

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts
    wishing to check a fields first characters match a given string.

    say lastloginip = "192.168.0.1"

    wishing to check the first part of lastloginip string, up to the length of $iptocheck string.

    please advise how i can do this using the following mysql statement.

    thanks


    Code:
    $array=explode(".",$ip);
    $iptocheckfor "$array[0].$array[1]";
    $res = @mysql_query("UPDATE `users` SET `lastlogindatetime` = '".$datetimenow."' WHERE `lastloginip` = '".$iptocheckfor."' LIMIT 1") or die(mysql_error());


  •  

    Posting Permissions

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