(PHP 4 >= 4.0.1) sscanf -- Parses input from a string according to a format Descriptionmixed sscanf (string str, string format [, string
var1...])
The function sscanf() is the input analog of
printf(). Sscanf() reads from
the string str and interprets it according to
the specified format. If only two parameters were
passed to this function, the values parsed will be returned as an array.
Esempio 1. Sscanf() Example
// getting the serial number
$serial = sscanf("SN/2350001","SN/%d");
// and the date of manufacturing
$mandate = "January 01 2000";
list($month, $day, $year) = sscanf($mandate,"%s %d %d");
echo "Item $serial was manufactured on: $year-".substr($month,0,3)."-$day\n";
|
|
If optional parameters are passed, the function will return the number of
assigned values. The optional parameters must be passed by reference.
Esempio 2. Sscanf() - using optional parameters
// get author info and generate DocBook entry
$auth = "24\tLewis Carroll";
$n = sscanf($auth,"%d\t%s %s", &$id, &$first, &$last);
echo "<author id='$id'>
<firstname>$first</firstname>
<surname>$last</surname>
</author>\n";
|
|
See also: fscanf(), printf(),
and sprintf().
| User Contributed Notes: sscanf |
shaun@phpboards.com
12-Sep-2000 04:26 |
It appears that sscanf() MUST be used in conjunction with list(), at least under 4.01. If I don't use list(), sscanf() will not properly assign a value.
The following code from the usage example
$serial = sscanf("SN/2350001","SN/%d");
echo $serial;
result in the output "Array" instead of the desired result. But when I add in the list() function,
list($serial) = sscanf("SN/2350001","SN/%d");
echo $serial;
the output is correct.
|
|
clcollie@mindspring.com
12-Sep-2000 04:59 |
Actually sscanf()_always_ returns an array if you specify less return variables than format specifiers. i may change this to return a scalar if only a single format specifier exists.
Note that sscanf() is (almost) the complete functional equivalent of its "C" counterpart, so you can do the following to get the expected effect :
sscanf("SN/2350001","SN/%d",&$serial)
The array return was a nicety for PHP.
|
|
owenh@halo5.com
28-Nov-2000 04:21 |
list($phone11, $phone12, $phone13) = sscanf($arow[9],"(%d) %d-%d");
If thoes variables dont have data in them it seems to cause segfaults on apache. ( just to save you some trouble shooting time here is my fix:
sscanf($arow[9],"(%d) %d-%d", &$phone11,&$phone12, &$phone13);
|
|
strasbg@ole.com
15-Feb-2001 11:44 |
Hay un error en la funcion sscanf ya que no realiza la lectura de datos segun el formato especificado. Por ejemplo, consideremos el siguiente codigo:
<?php
// este script es una prueba del scanf
$str="hola;esto;es;una;prueba";
$a="";
$b="";
$c="";
$d="";
$e="";
sscanf($str,"%s;%s;%s;%s;%s",&$a,&$b,&$c,&$d,&$e);
echo $a.$b.$c.$d.$e;
?>
La salida seria la siguiente:
hola;esto;es;una;prueba
Por el contrario si usamos el siguiente
formato:
<?php
// este script es una prueba del scanf
$str="hola ; esto ; es ; una ; prueba";
$a="";
$b="";
$c="";
$d="";
$e="";
sscanf($str,"%s ; %s ; %s ; %s ; %s",&$a,&$b,&$c,&$d,&$e);
echo $a.$b.$c.$d.$e;
?>
la salida es correcta:
holaestoesunaprueba
Se ve que la funcion sólo respeta
el formato si las zonas donde se
indica que comienza una variable
(en el codigo %(tipo_de_variable))
esta aislada, es decir, separada
con espacios del resto de los
caracteres del formato.
Saludos desde cordoba
|
|
|