PHP 5.6.16 is available


(PHP 5)

stripos Encontra a primeira ocorrencia de uma string sem diferenciar maiúsculas e minúsculas


int stripos ( string $haystack , string $needle [, int $offset ] )

Retorna a posição numerica da primeira ocorrencia de needle na string haystack. Diferentemente de strpos(), stripos() não diferencia maiúsculas e minúsculas.

Note que o parâmetro needle deve ser uma string de um ou mais caracteres.

Se needle não for encontrada, stripos() irá retornar boolean FALSE.


Esta função pode retornar o booleano FALSE, mas também pode retornar um valor não-booleano que pode ser avaliado como FALSE, como 0 ou "". Leia a seção em Booleanos para maiores informações. Utilize o operador === para testar o valor retornado por esta função.

Exemplo #1 Exemplos stripos()

$mystring1 'xyz';
$mystring2 'ABC';

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

// 'a' certamente não esta em 'xyz'
if ($pos1 === false) {
"A string '$findme' não foi encontrada na string '$mystring1'";

// Note o nosso uso de  ===. Simples == não funcionaria como o esperado
// porque a posição de 'a' é 0 (primeiro) caractere.
if ($pos2 !== false) {
"Nós encontramos '$findme' em '$mystring2' na posição $pos2";

Se needle não é uma string, é convertida para um inteiro e aplicado o valor do caractere.

O parâmetro opcional offset permite a você especificar apartir de qual caractere em haystack a procura começa. A posição retornada ainda é relativa ao inicio de haystack.

Veja também strpos(), strrpos(), strrchr(), substr(), stristr(), strripos(), strstr() e stri_replace().

add a note add a note

User Contributed Notes 6 notes

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

function multineedle_stripos($haystack, $needles, $offset=0) {
$needles as $needle) {
$found[$needle] = stripos($haystack, $needle, $offset);

// 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) {
spam at kleppinger dot com
8 months 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.
Ian Macdonald
3 months 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.
steve at opilo dot net
2 years ago
A handy function if you need to adjust layout based on whether or not a string contains descending letters:

<?php function containsDescenders($text) {
$descenders = array("g","j","p","q","y");
    foreach (
$descenders as $letter) {
        if (
stripos($text,$letter) !== false) {
spam at wikicms dot org
1 year ago
If you like using ternary operator, I wrote simple example how to use stripos function.
Also, in my example I add "How to use namespaces" for wide knowledges for newbies.

namespace My;

//You can be free using core functions in your NameSpaces (My)
function stripos($haystack, $needle) {
//To call core function (from global NS) you should add backslash only - \func
return (FALSE === \stripos($haystack, $needle)) ? FALSE : TRUE;

var_dump(stripos($haystack = 'John knows English language.', $needle = 'john')); //TRUE
var_dump(stripos($haystack = 'Sara knows English language too.', $needle = 'john')); //FALSE
grf at post dot cz
8 years ago
this would to work with any language, i hope.
tested on czech (eastern europe) lang.

* function finds and encase every string in a $needleArr array with
* strings $shearLft (from the left side) and $shearRgt (guess from which
* side).
* already encased needles are IGNORED for any other step, so order
* of needles in $needleArr is pretty important.
* function is searching needles in case-insensitive mode,
* but case in the subject is saved.
* can you do it better? so, do it.
* @param array $needleArr array of needles
* @param string $shearLft left shear
* @param string $shearRgt right shear
* @param string $subject subject
* @param string $encoding encoding ('utf-8' is default)
* @author griffin
function safeHighlight( $needleArr, $shearLft, $shearRgt, $subject, $encoding = 'utf-8')
// encoding
$e = $encoding;
// oh, no needles
if( !is_array( $needleArr))
// empty keys throw-off, only unique, reindex
$nA = array_values(
array_diff( $needleArr, array(''))
// needle count
if( !($nC = count( $nA)))
$subject; // nothing to hl
    // shear length
if( !(($rLL = mb_strlen( $rL = $shearLft, $e))
    + (
$rRL = mb_strlen( $rR = $shearRgt, $e))))
$subject; // no shears
    // subject length
if( !($sL = mb_strlen( $s = $subject, $e)))
null; // empty subject
    // subject in lowercase (we need to aviod
    // using mb_stripos due to PHP version)
$sW = mb_strtolower( $s, $e);
// masking ~ 0=not changed, 1=changed
$m = str_repeat( '0', $sL);
// loop for each needle
for( $n=0; $n<$nC; $n++)
// needle string loWercase
$nW = mb_strtolower( $nA[ $n], $e);
$o = 0; // offset
$nL = mb_strlen( $nW, $e); // needle length

        // search needle
while( false !== ($p = mb_strpos( $sW, $nW, $o, $e)))
// oh hurrey, needle found on $p position
            // is founded needle already modified? (in full-length)
for( $q=$p; $q<($p+$nL); $q++)
$m[ $q])
// ai, caramba. already modified, jump over
$o+= $nL;
// continue for while() loop - not for for() loop!
continue 2;
// explode subject and mask into three parts
            // partA|needle|partB
$sE[0] = mb_substr( $s, 0, $p, $e);
$sE[1] = mb_substr( $s, $p, $nL, $e);
$sE[2] = mb_substr( $s, $p+$nL, $sL-$p-$nL, $e);
// mask
            // partA|partB (needle not needed)
$mE[0] = mb_substr( $m, 0, $p, $e);
$mE[1] = mb_substr( $m, $p+$nL, $sL-$p-$nL, $e);
// apply shears
$sE[1] = $rL.$sE[1].$rR;
// update sunject length
$sL+= $rLL + $rRL;
// update mask
$m = $mE[0] . str_repeat( '1', $rLL + $nL + $rRL) . $mE[1];
// implode into a subject
$s = implode( $sE);
// update lowercase subject
$sW = mb_strtolower( $s, $e);
// increase offset
$o+= $rLL + $nL + $rRL;
// end of string reached
if( $o>=$sL)
// while()
} // for( $n=0; $n<$nC; $n++)
    // oouu yeaaa, kick the subject out of the function
return $s;
// function safeHighlight()
To Top