Re: Re: sscanf error?

From: Date: Sun, 07 Sep 2003 18:35:48 +0000
Subject: Re: Re: sscanf error?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-161921@lists.php.net to get a copy of this message
Hi, sscanf is meant to READ paramaters, which means it "transforms" the input into the desired output, which in your case it was an integer and an integer looses, of course the initial zeros "if any". What you wanted was, more or less, to transform the parameters, job for which sprintf is better suited, at least in this case. Cheers, Catalin "Nuno Lopes" <nunoplopes@sapo.pt> wrote in message news:001401c3756d$40be8a60$f80cc151@pc07653... > Hello, > > Thanks for your answer! > > You say that sscanf is doing it's job, so the documentation isn't very > clear. > I think it says that the format is equal to sprintf. And sprintf has %010d > in format that says that a var is a number with at least 10 digits, filled > with 0's. So I think that sscanf should do the job! > > > Nuno Lopes > > > > ----- Original Message ----- > From: "Catalin Trifu" <catalin@isp-software.de> > To: <php-general@lists.php.net> > Sent: Sunday, September 07, 2003 5:51 PM > Subject: [PHP] Re: sscanf error? > > > > Hi, > > > > sscanf does what it says, that is > > it reads parameters, which means it expects to find > > a string like this "33996344" and converts it > > into an integer; that's why you get the result you get. > > This code does the job, but it's ugly: :). > > > > <?php > > $db='33996344,33996351,"GB","GBR","UNITED KINGDOM" > > 50331648,83886079,"US","USA","UNITED STATES" > > 94585424,94585439,"SE","SWE","SWEDEN"'; > > > > $lines=explode("\n", $db); > > > > echo "<pre>"; > > print_r($lines); > > > > > > foreach ($lines as $line) { > > $args = explode(",", $line); > > $array = sprintf("%010d,%010d,%2s", $args[0], $args[1], $args[2]); > > $array = explode(",", $array); > > list ($start, $end, $country) = $array; > > print_r($array); > > } > > > > print_r(printf("%010d", "33996344")); > > echo "</pre>"; > > > > ?> > > It uses sprintf. > > > > Cheers, > > Catalin > > > > "Nuno Lopes" <nunoplopes@sapo.pt> wrote in message > > news:002201c3755b$d4a3e390$5a0ac151@pc07653... > > > Hello, > > > > > > I'm trying to use sscanf, but I'm not receiving the results I expected! > > Can > > > anybody help me, please? > > > > > > > > > Thanking in advance, > > > Nuno Lopes > > > > > > > > > /************************************************/ > > > <? > > > > > > $db='"33996344","33996351","GB","GBR","UNITED > > > KINGDOM" > > > "50331648","83886079","US","USA","UNITED > > > STATES" > > > > > > "94585424","94585439","SE","SWE","SWEDEN"'; > > > > > > $lines=explode("\n", $db); > > > > > > foreach ($lines as $line) { > > > $array=sscanf($line, "\"%010d\",\"%010d\",\"%2s"); > > > list ($start, $end, $country) = $array; > > > print_r($array); > > > } > > > ?> > > > /******************************/ > > > Output: > > > Array > > > ( > > > [0] => 33996344 > > > [1] => 33996351 > > > [2] => GB > > > ) > > > .... > > > /****************/ > > > Expected output: > > > Array > > > ( > > > [0] => 0033996344 > > > [1] => 0033996351 > > > [2] => GB > > > )

« previous php.general (#161921) next »