strspn
PHP Manual
PHP Manual»String Funzioni»strspn

strspn

(PHP 4, PHP 5, PHP 7, PHP 8)

strspn Trova la lunghezza di un testo che corrisponde alla maschera data

Descrizione

function strspn(
    string $str1,
    string $str2,
    int $start = ?,
    int $length = ?
): int

Restituisce la lunghezza di un segmento di str1 che contiene completamente i caratteri presenti in str2.

Il seguente codice:

<?php
$var = strspn("42 is the answer, what is the question ...", "1234567890");
?>
assegnerà 2 a $var, poiché la stringa "42" è il più lungo segmento contenente i caratteri da "1234567890".

Dal PHP 4.3.0, strspn() accetta altri due parametri interi opzionali: start che può essere usato per indicare la posizione di inizio da cui cercare, e length per indicare la lunghezza della stringa da esaminare.

<?php
echo strspn("foo", "o", 1, 2); // 2
?>

Nota: Questa funzione è binary-safe (gestisce correttamente i file binari)

Vedere anche strcspn().

To Top