strpos
Trova la posizione della prima occorrenza di una stringa
Descrizione
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.
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.