★ wanayoo — archive 1999 http://php.net/manual/it/function.sscanf.phpNouvelle recherche | Portail wanayoo
PHP Thursday, March 29, 2001  
downloads | documentation | faq | support | reporting bugs | links 

search for in the  

lookup:
Manuale PHPManuale PHP
StringsStrings
 AddCSlashesAddCSlashes
 AddSlashesAddSlashes
 bin2hexbin2hex
 ChopChop
 ChrChr
 chunk_splitchunk_split
 convert_cyr_stringconvert_cyr_string
 count_charscount_chars
 crc32crc32
 cryptcrypt
 echoecho
 explodeexplode
 get_html_translation_tableget_html_translation_table
 get_meta_tagsget_meta_tags
 hebrevhebrev
 hebrevchebrevc
 htmlentitieshtmlentities
 htmlspecialcharshtmlspecialchars
 implodeimplode
 joinjoin
 levenshteinlevenshtein
 ltrimltrim
 md5md5
 MetaphoneMetaphone
 nl2brnl2br
 OrdOrd
 parse_strparse_str
 printprint
 printfprintf
 quoted_printable_decodequoted_printable_decode
 quotemetaquotemeta
 rtrimrtrim
 sscanfsscanf
 setlocalesetlocale
 similar_textsimilar_text
 soundexsoundex
 sprintfsprintf
 strcasecmpstrcasecmp
 strchrstrchr
 strcmpstrcmp
 strcspnstrcspn
 strip_tagsstrip_tags
 stripcslashesstripcslashes
 stripslashesstripslashes
 stristrstristr
 strlenstrlen
 strnatcmpstrnatcmp
 strnatcasecmpstrnatcasecmp
 strncmpstrncmp
 str_padstr_pad
 strposstrpos
 strrchrstrrchr
 str_repeatstr_repeat
 strrevstrrev
 strrposstrrpos
 strspnstrspn
 strstrstrstr
 strtokstrtok
 strtolowerstrtolower
 strtoupperstrtoupper
 str_replacestr_replace
 strtrstrtr
 substrsubstr
 substr_countsubstr_count
 substr_replacesubstr_replace
 trimtrim
 ucfirstucfirst
 ucwordsucwords
 wordwrapwordwrap

previousrtrim
setlocalenext

Last updated: Sat, 17 Mar 2001
view this page in English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Spanish | Plain HTML

sscanf

(PHP 4 >= 4.0.1)

sscanf -- Parses input from a string according to a format

Description

mixed 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:

&lt;?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:

&lt;?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

previousrtrim
setlocalenext

Last updated: Sat, 17 Mar 2001
add note | about notes

show source | credits | stats | mirror sites:  

Copyright © 1998-2001 The PHP Group
All rights reserved.
This mirror generously provided by: chek.com
Last updated: Thu Mar 29 13:01:54 2001 EST