Re: sscanf error?

From: Date: Sun, 07 Sep 2003 16:51:17 +0000
Subject: Re: sscanf error?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-161914@lists.php.net to get a copy of this message
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 (#161914) next »