Descrizione
int
strpos ( string haystack, mixed needle [, int offset] )
Restituisce la posizione numerica della prima occorrenza di
needle nella stringa
haystack. Differentemente rispetto a
strrpos(), questa funzione considera tutta la stringa
needle, e, quindi, cercherà tutta
la stringa.
Se needle non viene trovato
strpos() restituirà boolean FALSE.
Avvertimento |
Questa funzione può
restituire il Booleano FALSE, ma può anche restituire un valore non-Booleano valutato
come FALSE, come ad esempio 0 o
"". Per favore fare riferimento alla sezione Booleans per maggiori
informazioni. Usare l'operatore
=== per controllare il valore restituito da questa
funzione. |
Nota: Questa funzione è
binary-safe (gestisce correttamente i file binari)
Esempio 1. Esempio di uso di strpos()
<?php $mystring = 'abc'; $findme = 'a'; $pos = strpos($mystring, $findme);
// Notate l'uso di ===. Il == non avrebbe risposto come atteso // poichè la posizione di 'a' è nel primo carattere. if ($pos === false) { echo "The string '$findme' was not found in the string '$mystring'"; } else { echo "The string '$findme' was found in the string '$mystring'"; echo " and exists at position $pos"; }
// Ricerca di un carattere ignorando qualsiasi cosa prima di offset $newstring = 'abcdef abcdef'; $pos = strpos($newstring, 'a', 1); // $pos = 7, not 0 ?>
|
|
Se needle non è una stringa, viene convertito
in un intero e utilizzato come valore ordinale di un carattere.
Il parametro opzionale offset permette di indicare
da quale carattere in haystack
iniziare la ricerca. La posizione restituita è, comunque, relativa
all'inizio di haystack.
Vedere anche strrpos(),
stripos(),
strripos(),
strrchr(),
substr(),
stristr() e
strstr().