LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php sscanf removing ":" from "1.1.1.1:22" string (https://www.linuxquestions.org/questions/programming-9/php-sscanf-removing-from-1-1-1-1-22-string-4175592077/)

MrUmunhum 10-23-2016 07:01 PM

php sscanf removing ":" from "1.1.1.1:22" string
 
Hi group,
I'm fighting with sscanf again. This is the script
Code:

#!/usr/bin/php

<?php

$Port = "Empty";
 $N = sscanf( "1.1.1.1:22 junk" , "%[^:]s:%s ", $IP, $Port );
  print "$N, $IP, $Port \n";

$N = sscanf( "1.1.1.1:22 junk" , "%s:%s ", $IP, $Port  );
  print "$N, $IP, $Port \n";

$N = sscanf( $IP , "%[^:]s%c%s ", $IP, $c, $Port  );
  print "$N, $IP, $c, $Port \n";

  exit();

?>

and I get these results
Code:

filter-1.php LOG-18Oct2016-Tuesday

1, 1.1.1.1, Empty
1, 1.1.1.1:22, Empty
1, 1.1.1.1, , Empty

Can anyone tell this idoit what the proper format is to remove the ":" and assign the Port number?

Thanks for your time.

michaelk 10-24-2016 07:44 AM

Try this...

Code:

$N = sscanf( "1.1.1.1:22 junk" , "%[^':']:%s", $IP, $Port );

MrUmunhum 10-25-2016 02:26 PM

Quote:

Originally Posted by michaelk (Post 5622241)
Try this...

Code:

$N = sscanf( "1.1.1.1:22 junk" , "%[^':']:%s", $IP, $Port );

OK, that works, as well as "%[^:]:%s", but why?
Does the "%[^:]" define a string? What if I wanted an integer? Seems "%d:%s " works but "%[^:]d:%s" does not!

Well, will wonders ever end?

Thanks for the fix.

michaelk 10-25-2016 03:35 PM

"%[^:]:%s" The [^:] means match everything from the beginning of the line except for the : which is assigned to $IP. I not a regular expression expert by any means but it is all about text matching so as far as I know it will default to text. The :%s will match everything after the : and assign it to $Port. It does work if it is a %d instead of a %s.

keefaz 10-25-2016 06:52 PM

Quote:

Originally Posted by MrUmunhum (Post 5622870)
OK, that works, as well as "%[^:]:%s", but why?
Does the "%[^:]" define a string? What if I wanted an integer?

It doesn't matter for PHP, I mean it's not a strictly typed language like C

ntubski 10-29-2016 09:08 PM

Quote:

Originally Posted by MrUmunhum (Post 5622870)
OK, that works, as well as "%[^:]:%s", but why?
Does the "%[^:]" define a string? What if I wanted an integer? Seems "%d:%s " works but "%[^:]d:%s" does not!

It's not documented in http://php.net/manual/en/function.sscanf.php(!?), but you can check the manual page for libc's scanf
https://manned.org/scanf.3
Code:

      [      Matches a nonempty sequence of characters from the specified set
              of accepted characters; [...] The set excludes those characters if the
              first character after the open bracket is a circumflex (^).

i.e., the %[^:] is its own format specifier, it doesn't combine with %s.

NevemTeve 10-30-2016 01:01 AM

@OP You should accept the fact that 'sscanf' isn't a universal lexical parser. Use it when possible, otherwise use other tools like function 'explode'.

MrUmunhum 11-01-2016 02:05 PM

Quote:

Originally Posted by NevemTeve (Post 5624731)
@OP You should accept the fact that 'sscanf' isn't a universal lexical parser. Use it when possible, otherwise use other tools like function 'explode'.

In my case, I am looking at very long lines and using explode would be very inefficient when I only need the first word.

Sscanf works when you can figure out the secret hand shakes.

Thanks for your time.


All times are GMT -5. The time now is 02:04 AM.