Best way to pull an IP out of string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard T. Cunningham

    Best way to pull an IP out of string?

    I've been using two instances of substr to pull an IP address out of a
    string. I know there's a more efficient way. Anyone? The string is
    like this:

    HOST 61.2.135.45 blocked.

    Richard

  • Ian.H

    #2
    Re: Best way to pull an IP out of string?

    On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:
    [color=blue]
    > I've been using two instances of substr to pull an IP address out of a
    > string. I know there's a more efficient way. Anyone? The string is
    > like this:
    >
    > HOST 61.2.135.45 blocked.
    >
    > Richard[/color]


    preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
    $ip = $matches[0];


    HTH =)



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK


    Comment

    • Garp

      #3
      Re: Best way to pull an IP out of string?


      "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
      news:pan.2004.0 4.10.02.08.56.4 69000@bubbleboy .digiserv.net.. .[color=blue]
      > On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:
      >[color=green]
      > > I've been using two instances of substr to pull an IP address out of a
      > > string. I know there's a more efficient way. Anyone? The string is
      > > like this:
      > >
      > > HOST 61.2.135.45 blocked.
      > >
      > > Richard[/color]
      >
      >
      > preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
      > $ip = $matches[0];
      >
      >
      > HTH =)
      >
      >
      >
      > Regards,
      >
      > Ian
      >
      > --
      > Ian.H
      > digiServ Network
      > London, UK
      > http://digiserv.net/
      >[/color]

      The only correct way is to pull the numbers out with an expression and test
      them for range validity manually. This works:

      function testipformatval id($input) {
      preg_match_all( '/\d{1,3}/',$input,$r);
      if(count($r[0]) != 4) return false;
      foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
      return true;
      }

      Garp


      Comment

      • Ian.H

        #4
        Re: Best way to pull an IP out of string?

        On Sat, 10 Apr 2004 09:49:29 +0000, Garp wrote:
        [color=blue]
        >
        > "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
        > news:pan.2004.0 4.10.02.08.56.4 69000@bubbleboy .digiserv.net.. .[color=green]
        >> On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:
        >>[color=darkred]
        >> > I've been using two instances of substr to pull an IP address out of a
        >> > string. I know there's a more efficient way. Anyone? The string is
        >> > like this:
        >> >
        >> > HOST 61.2.135.45 blocked.[/color][/color][/color]

        [color=blue][color=green]
        >> preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
        >> $ip = $matches[0];[/color][/color]

        [color=blue]
        > The only correct way is to pull the numbers out with an expression and test
        > them for range validity manually. This works:
        >
        > function testipformatval id($input) {
        > preg_match_all( '/\d{1,3}/',$input,$r);
        > if(count($r[0]) != 4) return false;
        > foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
        > return true;
        > }[/color]


        But that wasn't part of the question ;)

        From the OP.. it would appear this would be from a log file of some kind
        inwhich you can almost guarantee that the IP address will be a valid IP
        address and not need to have each of its octects parsed.

        Also, please check your function.. I fail to see where it actually pulls
        the IP address out (ie: as I had it stored in $ip) which appears to be
        what the OP was asking for.. not to validate each octet.



        Regards,

        Ian

        --
        Ian.H
        digiServ Network
        London, UK


        Comment

        • Garp

          #5
          Re: Best way to pull an IP out of string?


          "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
          news:pan.2004.0 4.10.10.23.27.7 66000@bubbleboy .digiserv.net.. .[color=blue]
          > On Sat, 10 Apr 2004 09:49:29 +0000, Garp wrote:
          >[color=green]
          > >
          > > "Ian.H" <ian@WINDOZEdig iserv.net> wrote in message
          > > news:pan.2004.0 4.10.02.08.56.4 69000@bubbleboy .digiserv.net.. .[color=darkred]
          > >> On Fri, 09 Apr 2004 19:00:01 -0700, Richard T. Cunningham wrote:
          > >>
          > >> > I've been using two instances of substr to pull an IP address out of[/color][/color][/color]
          a[color=blue][color=green][color=darkred]
          > >> > string. I know there's a more efficient way. Anyone? The string is
          > >> > like this:
          > >> >
          > >> > HOST 61.2.135.45 blocked.[/color][/color]
          >
          >[color=green][color=darkred]
          > >> preg_match("/\s[0-9.]+\s/", "HOST 61.2.135.45 blocked", $matches);
          > >> $ip = $matches[0];[/color][/color]
          >
          >[color=green]
          > > The only correct way is to pull the numbers out with an expression and[/color][/color]
          test[color=blue][color=green]
          > > them for range validity manually. This works:
          > >
          > > function testipformatval id($input) {
          > > preg_match_all( '/\d{1,3}/',$input,$r);
          > > if(count($r[0]) != 4) return false;
          > > foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
          > > return true;
          > > }[/color]
          >
          >
          > But that wasn't part of the question ;)
          >
          > From the OP.. it would appear this would be from a log file of some kind
          > inwhich you can almost guarantee that the IP address will be a valid IP
          > address and not need to have each of its octects parsed.
          >
          > Also, please check your function.. I fail to see where it actually pulls
          > the IP address out (ie: as I had it stored in $ip) which appears to be
          > what the OP was asking for.. not to validate each octet.
          >
          >
          >
          > Regards,
          >
          > Ian
          >
          > --
          > Ian.H
          > digiServ Network
          > London, UK
          > http://digiserv.net/
          >[/color]

          You already answered his question, I was providing extra services. 8) I did
          typo tho:
          The only correct way is to pull the numbers out ...
          should be
          The only correct way to validate the address is to pull the numbers out
          ....

          Garp


          Comment

          • Ian.H

            #6
            Re: Best way to pull an IP out of string?

            On Sat, 10 Apr 2004 10:26:50 +0000, Garp wrote:


            [ snip ]

            [color=blue][color=green]
            >> Also, please check your function.. I fail to see where it actually pulls
            >> the IP address out (ie: as I had it stored in $ip) which appears to be
            >> what the OP was asking for.. not to validate each octet.[/color][/color]

            [color=blue]
            >
            > You already answered his question, I was providing extra services. 8) I did
            > typo tho:
            > The only correct way is to pull the numbers out ...
            > should be
            > The only correct way to validate the address is to pull the numbers out[/color]


            Fair play.. makes more sense now with the typo "correction " =)



            Regards,

            Ian

            --
            Ian.H
            digiServ Network
            London, UK


            Comment

            • Chung Leong

              #7
              Re: Best way to pull an IP out of string?


              "Garp" <garp7@no7.blue yonder.co.uk> wrote in message
              news:JqPdc.375$ P32.3896422@new s-text.cableinet. net...[color=blue]
              > The only correct way is to pull the numbers out with an expression and[/color]
              test[color=blue]
              > them for range validity manually. This works:
              >
              > function testipformatval id($input) {
              > preg_match_all( '/\d{1,3}/',$input,$r);
              > if(count($r[0]) != 4) return false;
              > foreach($r[0] as $s) if($s < 0 || $s > 255) return false;
              > return true;
              > }[/color]

              Why roll your own function when there's a built-in one?

              if(ip2long($add ress) < 0) {
              echo "Invalid IP address!";
              }


              Comment

              • Richard T. Cunningham

                #8
                Re: Best way to pull an IP out of string?

                On Sat, 10 Apr 2004 10:31:00 GMT, "Ian.H" <ian@WINDOZEdig iserv.net>
                wrote:
                [color=blue]
                >On Sat, 10 Apr 2004 10:26:50 +0000, Garp wrote:
                >
                >
                >[ snip ]
                >
                >[color=green][color=darkred]
                >>> Also, please check your function.. I fail to see where it actually pulls
                >>> the IP address out (ie: as I had it stored in $ip) which appears to be
                >>> what the OP was asking for.. not to validate each octet.[/color][/color]
                >
                >[color=green]
                >>
                >> You already answered his question, I was providing extra services. 8) I did
                >> typo tho:
                >> The only correct way is to pull the numbers out ...
                >> should be
                >> The only correct way to validate the address is to pull the numbers out[/color]
                >
                >
                >Fair play.. makes more sense now with the typo "correction " =)
                >
                >
                >
                >Regards,
                >
                > Ian[/color]


                Thanks to both of you!

                Richard

                Comment

                • Sandman

                  #9
                  Re: Best way to pull an IP out of string?

                  In article <u4le70ha0hcl44 gsfbc9atldegn5h 19q28@4ax.com>,
                  Richard T. Cunningham <nospam@nospam. com> wrote:
                  [color=blue]
                  > I've been using two instances of substr to pull an IP address out of a
                  > string. I know there's a more efficient way. Anyone? The string is
                  > like this:
                  >
                  > HOST 61.2.135.45 blocked.
                  >
                  > Richard[/color]

                  As Ian said, or you could us a general preg_match for ip's like so:

                  preg_match("/(\d{1,3}\.\d{1, 3}\.\d{1,3}\.\d {1,3})/", $string, $matches)

                  That will put any occurance of a ip number (x.x.x.x, xx.xx.xx.xx,
                  xxx.xxx.xxx.xxx or any mix) in $matches (even invalid ones, such as
                  666.666.666.666 ).

                  --
                  Sandman[.net]

                  Comment

                  Working...