downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

stripslashes> <stripcslashes
[edit] Last updated: Fri, 18 Sep 2009

view this page in

stripos

(PHP 5)

striposВръща позицията на първата поява на подниз в низ, без да се отчита регистъра

Описание

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

Връща позицията на първата поява на needle в haystack низа.

За разлика от strpos(), stripos() е нечувствителна към регистъра.

Параметри

haystack

Низът в който се извършва търсенето

needle

Забележете, че needle може да бъде низ от един или повече знака.

Ако needle не е низ, се преобразува в целочислен вид и се третира като код на символа.

offset

Незадължителният параметър offset ви позволява да укажете от кой знак в haystack да започне търсенето. Върнатата позиция ще е все още от началото на haystack .

Връщани стойности

Ако низът needle не бъде открит, strpos() ще върне FALSE.

Предупреждение

Тази функция може да върне булева стойност FALSE, но може също да върне небулева стойност, която се интерпретира като FALSE, като 0 или "". Вижте раздел Булев тип за повече информация. Използвайте оператора === за проверка на връщаната стойност от тази функция.

Примери

Example #1 Примери за stripos()

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

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

// Не, 'a' а определено не е в 'xyz'
if ($pos1 === false) {
    echo 
"Низът '$findme' не беше открит в низа '$mystring1'";
}

// Забележете употребата на оператора ===.  Обикновеното сравняване с оператора ==
// няма да работи както се очаква, тъй като позицията на 'a' е нулевият (първи) знак.
if ($pos2 !== false) {
    echo 
"Отхрихме '$findme' в'$mystring2' на позиция $pos2";
}
?>

Бележки

Забележка: Тази функция е двоично сигурна (binary-safe).

Вж. също

  • strpos() - Връща позицията на първата поява на подниз
  • strrpos() - Връща позицията на последната поява на знак в низ
  • strrchr() - Връща последната поява на знак в низ
  • substr() - Връща част от низ
  • stristr() - Нечувствителен към регистъра вариант на функция strstr
  • strstr() - Връща първата поява на низ
  • strripos() - Връща позицията на последната поява на подниз в низ, без да се отчита регистъра
  • str_ireplace() - Нечувствителна към регистъра версия на str_replace



add a note add a note User Contributed Notes stripos - [2 notes]
up
2
emperorshishire at gmail dot com
4 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
0
grf at post dot cz
6 years ago
this would to work with any language, i hope.
tested on czech (eastern europe) lang.

<?php
/****************************************
*    SAFE HIGHLIGHT
****************************************/
/**
 * 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))
        return
$subject;
       
   
// empty keys throw-off, only unique, reindex
   
$nA = array_values(
           
array_unique(
               
array_diff( $needleArr, array(''))
            )
        );
   
   
// needle count
   
if( !($nC = count( $nA)))
        return
$subject; // nothing to hl
       
    // shear length
   
if( !(($rLL = mb_strlen( $rL = $shearLft, $e))
    + (
$rRL = mb_strlen( $rR = $shearRgt, $e))))
        return
$subject; // no shears
   
    // subject length
   
if( !($sL = mb_strlen( $s = $subject, $e)))
        return
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++)
                if(
$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)
                break;
           
        }
// while()
       
   
} // for( $n=0; $n<$nC; $n++)
   
    // oouu yeaaa, kick the subject out of the function
   
return $s;
   
}
// function safeHighlight()
/****************************************
*    END: SAFE HIGHLIGHT
****************************************/
?>

 
show source | credits | sitemap | contact | advertising | mirror sites