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

search for in the

stripslashes> <stripcslashes
Last updated: Fri, 24 Jul 2009

view this page in

stripos

(PHP 5)

stripos대소문자를 구분 없이 문자열이 처음 나타나는 위치를 탐색

설명

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

haystack string에서 needle 이 처음 나타나는 위치를 반환합니다.

strpos()와 달리, stripos()는 대소문자를 구분하지 않습니다.

인수

haystack

검색할 문자열

needle

needle 는 한 문자 이상의 문자열이여야 합니다.

needle 이 문자열이 아니라면, 정수로 변환하여 해당하는 값의 문자를 적용합니다.

offset

선택적인 offset 인수로 haystack 의 몇번째 문자에서 검색을 시작할지 지정할 수 있습니다. 반환하는 위치는 haystack 의 처음부터 세어집니다.

반환값

needle 을 발견하지 못하면, stripos()boolean FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 0이나 "" 등, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

예제

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'가 0번째
// (처음) 문자기에, 기대하는대로 작동하지 않습니다.
if ($pos2 !== false) {
    echo 
"'$findme'를 '$mystring2' 위치 $pos2에서 찾았습니다.";
}
?>

주의

Note: 이 함수는 바이너리 안전입니다.

참고

  • strpos() - 문자열이 처음 나타나는 위치를 찾습니다
  • strrpos() - 문자열에서 마지막 문자의 위치를 찾습니다
  • strrchr() - 문자열에서 문자가 마지막으로 나오는 부분을 찾습니다
  • substr() - Return part of a string
  • stristr() - 대소문자를 구분하지 않는 strstr
  • strstr() - 문자열이 처음으로 나오는 부분을 찾습니다
  • stripos()
  • str_ireplace() - 대소문자 구분 없는 str_replace



stripslashes> <stripcslashes
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
stripos
vgiralt
19-Aug-2009 06:38
The mail validator example is NON CONFORMANT with SMTP mail RFCs. Valid characters for Internet e-mail address are more than the ones listed here. Implementing such lame validator is a big disservice to the whole Internet.

Those interested can read RFC 5322. In short, the valid characters are different on both sides of the @ sign, so, before applying this fast technique the address should be exploded into the two side strings, and then, use different valid character strings for the local-part and the domain.

A local part can be any alphabetic or digit US-ASCII character plus "!#$%&'*+-/=?^_`{|}~", and then, if it is enclosed in double-quotes ("), it can be any US-ASCII character except double-quote (") and back-slash(\).

The domain can be any printable US-ASCII characters not including "[", "]", or "\".

And then, this could be overridden with the advent of internationalized domains, that can use UTF-8 character sets.

Implementation of the validator is left as an exercise for the reader.
manualregexp
18-May-2009 07:52
Fast email validator using stripos to check for valid characters.

<?php
function validate_email($value) {
  
$chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ".
                
"abcdefghijklmnopqrstuvwxyz0123456789@._";
  
$at=0/* at sign */
  
$dot=0; /* dot after at */
  
$end_pos = strlen($value)-1;

   for (
$i=0;$i<strlen($value);$i++) {
     
$c = $value[$i];
      if (
stripos($chars, $c)===false) return false;
      if (
$c=="@") $at++;
      if (
$at==1 && $c==".") $dot++;
      if (
$at>1) return false;
     
/* Don't start or end with an '@' or a '.'
       * No @'s or .'s next to each other.
       */
     
if (($c=="." || $c=="@") &&
             (
$i==0 || $i==$end_pos || $prev_c=="." || $prev_c=="@"))
              return
false;
     
$prev_c=$c;
   }
   return (
$at==1 && $dot>0);
}
?>
emperorshishire at gmail dot com
25-Feb-2009 12:31
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)
   }
*/
?>
Ysangkok
07-Mar-2008 12:44
@henke:

No. It does NOT make a warning if the needle isn't found. This is false.
henke at henke37 dot cjb dot net
10-Dec-2007 03:48
This function will raise a warning if the needle was not found. Be sure to use @ to suppress it if you want the strpos like behavior of only returning false.
hpavon at gmail dot com
19-Jul-2007 04:54
To shane_at_51smith-dot-nl
Fine but not useful if you want to use stripos 3rd param òffset´ for instance to find all occurences of `needle´.
Again rchillet and heavyraptor solution is best though you'd better keep to-lowercase strings outside the function if you're planning to loop through `haystack´ like this:

$lower_haystack = strtolower($haystack);
$lower_needle = strtolower($needle);
$offset = 0;
//... looping over haystack
while ($offset !== false) {
    $offset = strpos($lower_haystack, $lower_needle, $offset);
    //... some funny stuff here
}

Using rchillet-heavyraptor function here would slow down performance. Solution: don't invent any function
shane_at_51smith-dot-nl
04-Jul-2007 04:46
Yet another stripos for php4 alternative:
<?php
function stripos($haystack, $needle){
    return
strpos($haystack, stristr( $haystack, $needle ));
}
$strTest = "The brown fox jumped over the Fence";
echo
stripos($strTest, 'fence');//30
?>
grf at post dot cz
20-Mar-2007 09:57
(i'm very sorry for some mistakes in my yesterday's post :'[ )

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
****************************************/
?>
dazzle
16-Aug-2006 11:20
I recommend using the solution by rchillet for <PHP5

michiels solution is quite slow if you use it for long strings or call it many times.
michiel at mb-it dot nl
10-Jul-2006 08:03
Since the stripos-function is PHP5-only, the function below could give PHP4-users the same functionallity:

function stripos($string,$word)
{
   $retval = false;
   for($i=0;$i<=strlen($string);$i++)
   {
       if (strtolower(substr($string,$i,strlen($word))) == strtolower($word))
       {
           $retval = true;
       }
   }
   return $retval;
}
rchillet at hotmail dot com
28-Apr-2006 05:45
improvement the function of heavyraptor with int offset parametre.

if (!function_exists("stripos")) {
  function stripos($str,$needle,$offset=0)
  {
      return strpos(strtolower($str),strtolower($needle),$offset);
  }
}
heavyraptor
21-Mar-2006 11:59
If you're using PHP < 5, you can use this alternate function, same thing like stripos:

<?php
if (!function_exists("stripos")) {
  function
stripos($str,$needle) {
   return
strpos(strtolower($str),strtolower($needle));
  }
}
?>

Such a logical function but I hope it helps ...
sim
03-Oct-2004 02:17
Just to be explicit, the position index returned by strpos starts at 0, not 1. e.g. strpos('abc','a') returns 0.
aidan at php dot net
31-May-2004 02:36
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat

stripslashes> <stripcslashes
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites