473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with sscanf

I'm obviously missing something here. I have a line like this (in
$line), and I want all the text within the double quotes::

somestr="string I want"

with some whitespace at the beginning. So I try this:

$res = sscanf ($line, ' %s"%s"');
$wanted = $res[1];

This appears to put the whole string in $res[0] (possibly minus the
whitespace). What am I doing wrong?

TIA - tim
Jun 27 '08 #1
9 2040
Tim Streater schrieb:
I'm obviously missing something here. I have a line like this (in
$line), and I want all the text within the double quotes::

somestr="string I want"

with some whitespace at the beginning. So I try this:

$res = sscanf ($line, ' %s"%s"');
$wanted = $res[1];

This appears to put the whole string in $res[0] (possibly minus the
whitespace). What am I doing wrong?

TIA - tim
You are looking for:
preg_match_all();
Jun 27 '08 #2
Tim Streater escribió:
$res = sscanf ($line, ' %s"%s"');
$wanted = $res[1];

This appears to put the whole string in $res[0] (possibly minus the
whitespace). What am I doing wrong?
Well, that depends on what $line looks like. Can we see it?
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 27 '08 #3
On 24 Jun, 15:08, "Álvaro G. Vicario"
<alvaroNOSPAMTHA...@demogracia.comwrote:
Tim Streater escribió:
$res = sscanf ($line, ' %s"%s"');
$wanted = $res[1];
This appears to put the whole string in $res[0] (possibly minus the
whitespace). What am I doing wrong?

Well, that depends on what $line looks like. Can we see it?
He told you in the OP
Jun 27 '08 #4
Captain Paralytic escribió:
>>$res = sscanf ($line, ' %s"%s"');
$wanted = $res[1];
This appears to put the whole string in $res[0] (possibly minus the
whitespace). What am I doing wrong?
Well, that depends on what $line looks like. Can we see it?
He told you in the OP
I guess you're right; I was confused because I was expecting actual
code, not a description of it xD
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 27 '08 #5
In article
<e0**********************************@d1g2000hsg.g ooglegroups.com>,
Captain Paralytic <pa**********@yahoo.comwrote:
On 24 Jun, 15:08, "Álvaro G. Vicario"
<alvaroNOSPAMTHA...@demogracia.comwrote:
Tim Streater escribió:
$res = sscanf ($line, ' %s"%s"');
$wanted = $res[1];
This appears to put the whole string in $res[0] (possibly minus the
whitespace). What am I doing wrong?
Well, that depends on what $line looks like. Can we see it?
He told you in the OP
I since discover there may be a NULL at the end of the string. But I
wouldn't have expected that to matter.

Meanwhile I have used strtok to get the bit I want. It would be nice to
know what was going on, however.
Jun 27 '08 #6
In article <g3**********@huron.algomas.org>,
"Álvaro G. Vicario" <al****************@demogracia.comwrote:
Captain Paralytic escribió:
>$res = sscanf ($line, ' %s"%s"');
$wanted = $res[1];
This appears to put the whole string in $res[0] (possibly minus the
whitespace). What am I doing wrong?
Well, that depends on what $line looks like. Can we see it?
He told you in the OP

I guess you're right; I was confused because I was expecting actual
code, not a description of it xD
That *was* actual code :-)
Jun 27 '08 #7
Tim Streater escribió:
somestr="string I want"

with some whitespace at the beginning. So I try this:

$res = sscanf ($line, ' %s"%s"');
I'd say that sscanf() performs a greedy match so the first %s receives
as much as it cans, given that it fits into the description of "string"
(e.g., everything) and there's nothing left for the second %s.

It's time you get to know regular expressions. An untested hint:

if( preg_match('/^\s+(\S+)"(.*)"$/iU', $line, $matches) ){
print_r( $matches );
}

P.S. I didn't even know there was scanf in PHP; its C variant is the
most useless function I learnt in my programming classes :)

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 27 '08 #8
In article <g3**********@huron.algomas.org>,
"Álvaro G. Vicario" <al****************@demogracia.comwrote:
Tim Streater escribió:
somestr="string I want"

with some whitespace at the beginning. So I try this:

$res = sscanf ($line, ' %s"%s"');

I'd say that sscanf() performs a greedy match so the first %s receives
as much as it cans, given that it fits into the description of "string"
(e.g., everything) and there's nothing left for the second %s.
That seems to be what it's doing even though that contradicts the
documentation. An example:

$line = "One two";
$res = sscanf ($line, "%s %s")
echo $res[0] // outputs "One"
echo $res[1] // outputs "Two"

This is similar to the examples in the on-line docs.
It's time you get to know regular expressions. An untested hint:

if( preg_match('/^\s+(\S+)"(.*)"$/iU', $line, $matches) ){
print_r( $matches );
}
I avoid regexps unless I am forced to by the function involved, and even
then keep it simple. Regexps are typically unreadable.
P.S. I didn't even know there was scanf in PHP; its C variant is the
most useless function I learnt in my programming classes :)
sscanf. I've used it to great effect in the past in C code, but that was
20 years ago. Perhaps the PHP version is broken.
Jun 27 '08 #9
On Tue, 24 Jun 2008 07:51:42 -0700, Tim Streater
<ti**********@dante.org.ukwrote:
In article <g3**********@huron.algomas.org>,
"Álvaro G. Vicario" <al****************@demogracia.comwrote:
>Tim Streater escribió:
somestr="string I want"

with some whitespace at the beginning. So I try this:

$res = sscanf ($line, ' %s"%s"');

I'd say that sscanf() performs a greedy match so the first %s receives
as much as it cans, given that it fits into the description of "string"
(e.g., everything) and there's nothing left for the second %s.

That seems to be what it's doing even though that contradicts the
documentation. An example:

$line = "One two";
$res = sscanf ($line, "%s %s")
echo $res[0] // outputs "One"
echo $res[1] // outputs "Two"

This is similar to the examples in the on-line docs.
>It's time you get to know regular expressions. An untested hint:

if( preg_match('/^\s+(\S+)"(.*)"$/iU', $line, $matches) ){
print_r( $matches );
}

I avoid regexps unless I am forced to by the function involved, and even
then keep it simple. Regexps are typically unreadable.
That's good practice, but regexes need not be unreadable, that's what the
/x modifier is for.
>P.S. I didn't even know there was scanf in PHP; its C variant is the
most useless function I learnt in my programming classes :)

sscanf. I've used it to great effect in the past in C code, but that was
20 years ago. Perhaps the PHP version is broken.
I was messing around with sscanf, this seems to work, but you need to know
the exact whitespace at the beginning. I also had to assume that you used
foo="bar" exactly. If you need better matching, regex is your best bet,
it's not that much overhead, anyway.

sscanf($str, ' %[a-z0-9]="%[a-z0-9]"', $name, $value);

--
Curtis, http://dyersweb.com
Jul 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
4
by: Ivan Lam | last post by:
Hi All, Thanks for reading my post! I have a problem that using the scanf function. I would like to scan a value from a line like: file:c:\program files\mpd\mpd.exe however, when I read...
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
10
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
4
by: lynx_ace | last post by:
Hi everyone. I need a little bit help here...I have an assignment and it is working fine until the last part which I can't solve. So here's the code in simple form #define maxlength 200 ...
20
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since 2004? Thanks Mike
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
4
by: utab | last post by:
Dear all, I have to interface some C code in C++, but I had a problem with sscanf function, it has been some time I have not used C and I could not figure out my problem. Simple code is below, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.