Esercitazione PHP

PHP HOME Introduzione a PHP Installazione PHP Sintassi PHP Commenti PHP Variabili PHP PHP Eco/Stampa Tipi di dati PHP Stringhe PHP Numeri PHP PHP matematica Costanti PHP Operatori PHP PHP Se...Altro...Altro Passaggio PHP Ciclo PHP Funzioni PHP Matrici PHP Superglobali PHP RegEx PHP

Moduli PHP

Gestione dei moduli PHP Convalida del modulo PHP Modulo PHP richiesto URL/e-mail del modulo PHP Modulo PHP completo

PHP avanzato

Data e ora PHP PHP Include Gestione dei file PHP Apri/Leggi file PHP Creazione/scrittura di file PHP Caricamento file PHP Cookie PHP Sessioni PHP Filtri PHP Filtri PHP avanzati Funzioni di callback PHP PHP JSON Eccezioni PHP

PHP OOP

PHP Che cos'è OOP Classi/Oggetti PHP Costruttore PHP PHP distruttore Modificatori di accesso PHP Ereditarietà PHP Costanti PHP Classi astratte PHP Interfacce PHP Tratti PHP Metodi statici PHP Proprietà statiche PHP Spazi dei nomi PHP Iterabili PHP

Database MySQL

Database MySQL MySQL Connect MySQL Crea DB MySQL Crea tabella Dati di inserimento MySQL MySQL Ottieni l'ultimo ID MySQL inserisce più MySQL preparato MySQL Seleziona dati MySQL dove MySQL Ordina per MySQL Elimina dati Dati di aggiornamento MySQL Dati limite MySQL

PHP XML

Parser XML PHP Analizzatore PHP SimpleXML PHP SimpleXML - Ottieni PHP XML espatriato PHP XML DOM

PHP - AJAX

Introduzione all'Ajax AJAX PHP Database AJAX XML AJAX Ricerca in tempo reale AJAX Sondaggio AJAX

Esempi PHP

Esempi PHP compilatore PHP Quiz PHP Esercizi PHP Certificato PHP

Riferimento PHP

Panoramica di PHP matrice PHP Calendario PHP Data PHP Directory PHP Errore PHP Eccezione PHP File system PHP Filtro PHP PHP FTP PHP JSON Parole chiave PHP PHP Libxml Posta PHP PHP matematica PHP Varie PHP MySQLi Rete PHP Controllo dell'output PHP RegEx PHP PHP SimpleXML Flusso PHP Stringa PHP Gestione delle variabili PHP Analizzatore XML PHP Zip PHP Fuso orario PHP

Funzione PHP sscanf()

❮ Riferimento alla stringa PHP

Esempio

Analizza una stringa:

<?php
$str = "age:30 weight:60kg";
sscanf($str,"age:%d weight:%dkg",$age,$weight);
// show types and values
var_dump($age,$weight);
?>

La funzione sscanf() analizza l'input da una stringa in base a un formato specificato. La funzione sscanf() analizza una stringa in variabili in base alla stringa di formato.

Se vengono passati solo due parametri a questa funzione, i dati verranno restituiti come una matrice. In caso contrario, se vengono passati parametri facoltativi, i dati analizzati vengono archiviati in essi. Se sono presenti più specificatori che variabili per contenerli, si verifica un errore. Tuttavia, se sono presenti meno specificatori rispetto alle variabili, le variabili aggiuntive contengono NULL.

Funzioni correlate:

  • printf() - restituisce una stringa formattata
  • sprintf() - scrive una stringa formattata in una variabile

Sintassi

sscanf(string,format,arg1,arg2,arg++)

Valori dei parametri

Parameter Description
string Required. Specifies the string to read
format Required. Specifies the format to use.

Possible format values:

  • %% - Returns a percent sign
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Additional format values. These are placed between the % and the letter (example %.2f):

  • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
  • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
  • - (Left-justifies the variable value)
  • [0-9] (Specifies the minimum width held of to the variable value)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)

Note: If multiple additional format values are used, they must be in the same order as above.

arg1 Optional. The first variable to store data in
arg2 Optional. The second variable to store data in
arg++ Optional. The third, fourth, and so on, to store data in


Dettagli tecnici

Valore di ritorno: Se vengono passati solo due parametri a questa funzione, i dati verranno restituiti come una matrice. In caso contrario, se vengono passati parametri facoltativi, i dati analizzati vengono archiviati in essi. Se sono presenti più specificatori che variabili per contenerli, si verifica un errore. Tuttavia, se sono presenti meno specificatori rispetto alle variabili, le variabili aggiuntive contengono NULL.
Versione PHP: 4.0.1+

Altri esempi

Esempio

Utilizzando i valori di formato %s, %d e %c:

<?php
$str = "If you divide 4 by 2 you'll get 2";
$format = sscanf($str,"%s %s %s %d %s %d %s %s %c");
print_r($format);
?>

❮ Riferimento alla stringa PHP