PHP 8.3.4 Released!

stripos

(PHP 5, PHP 7, PHP 8)

stripos Trova la prima occorrenza in una stringa senza distinzione tra maiuscole e minuscole

Descrizione

stripos(string $haystack, string $needle, int $offset = ?): int

Restituisce la posizione numerica della prima occorrenza di needle nella stringa haystack. Differentemente da strpos(), stripos() non distingue tra maiuscole e minuscole.

Occorre rilevare che needle può essere una stringa di uno o più caratteri.

Se needle non viene trovato, stripos() restituirà boolean false.

Avviso

Questa funzione può restituire il Booleano false, ma può anche restituire un valore non-Booleano valutato come false. Fare riferimento alla sezione Booleans per maggiori informazioni. Usare l'operatore === per controllare il valore restituito da questa funzione.

Example #1 Esempi di uso di stripos()

<?php
$findme
= 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';

$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);

// No, 'a' non è certamente in 'xyz'
if ($pos1 === false) {
echo
"The string '$findme' was not found in the string '$mystring1'";
}

// Notate l'uso di ===. Semplicemente == non avrebbe funzionato come atteso
// perché la posizione di 'a' è nel carattere 0 (il primo).
if ($pos2 !== false) {
echo
"We found '$findme' in '$mystring2' at position $pos2";
}
?>

Se needle non è una stringa, sarà convertito in un intero e utilizzato come valore ordinale di un carattere.

Il parametro opzionale offset permette di indicare da quale carattere di haystack iniziare la ricerca. La posizione restituita sarà relativa all'inizio di haystack.

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

Vedere anche strpos(), strrpos(), strrchr(), substr(), stristr(), strstr(), strripos() e str_ireplace().

add a note

User Contributed Notes 5 notes

up
42
emperorshishire at gmail dot com
15 years ago
I found myself needing to find the first position of multiple needles in one haystack. So I wrote this little function:

<?php
function multineedle_stripos($haystack, $needles, $offset=0) {
foreach(
$needles as $needle) {
$found[$needle] = stripos($haystack, $needle, $offset);
}
return
$found;
}

// It works as such:
$haystack = "The quick brown fox jumps over the lazy dog.";
$needle = array("fox", "dog", ".", "duck")
var_dump(multineedle_stripos($haystack, $needle));
/* Output:
array(3) {
["fox"]=>
int(16)
["dog"]=>
int(40)
["."]=>
int(43)
["duck"]=>
bool(false)
}
*/
?>
up
8
sorrynorealemail at example dot com
5 years ago
Unlike strpos() it seems that stripos() does NOT issue a WARNING if the needle is an empty string ''.
up
6
spam at kleppinger dot com
9 years ago
Regarding the function by spam at wikicms dot org

It is very bad practice to use the same function name as an existing php function but have a different output format. Someone maintaining the code in the future is likely to be very confused by this. It will also be hard to eradicate from a codebase because the naming is identical so each use of stripos() would have to be analyzed to see how it is expecting the output format (bool or number/bool).

Calling it string_found() or something like that would make a lot more sense for long-term use.
up
3
emanuel dot karlsson at rolfsbuss dot se
5 years ago
Finding numbers in strings requires you to cast the number to string first.

strpos("123", 2) !== strpos("123", "2")
up
4
Ian Macdonald
8 years ago
Regarding the === note, it might be worth clarifying that the correct tests for a binary found/not found condition are !==false to detect found, and ===false to detect not found.
To Top