LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2016, 07:01 PM   #1
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Rep: Reputation: 40
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.
 
Old 10-24-2016, 07:44 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Try this...

Code:
$N = sscanf( "1.1.1.1:22 junk" , "%[^':']:%s", $IP, $Port );
 
Old 10-25-2016, 02:26 PM   #3
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by michaelk View Post
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.
 
Old 10-25-2016, 03:35 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
"%[^:]:%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.
 
Old 10-25-2016, 06:52 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by MrUmunhum View Post
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
 
Old 10-29-2016, 09:08 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by MrUmunhum View Post
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.
 
Old 10-30-2016, 01:01 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@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'.
 
Old 11-01-2016, 02:05 PM   #8
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by NevemTeve View Post
@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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] X: "loading extension glx" "no screens found" "fatal server error" (w/ nvidia driver) Geremia Slackware 7 12-29-2014 11:00 AM
[SOLVED] "net rpc" "failed to connect to ipc$ share on" or "unable to find a suitable server" larieu Linux - General 0 11-09-2014 12:45 AM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
Perl - Can't use string ("html") as an ARRAY ref while "strict refs" OldGaf Programming 9 08-11-2009 11:14 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration