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
stripos
(PHP 5)
stripos — 大文字小文字を区別せずに文字列が最初に現れる位置を探す
説明
int stripos ( string $haystack, string $needle [, int $offset] )文字列 haystack の中で needle が最初に現れる位置を数字で返します。 strpos() と異なり、stripos() は大文字小文字を区別しません。
needle は、ひとつまたは複数の文字であることに 注意しましょう。
needle がみつからない場合、 strpos() は boolean FALSE を返します。
この関数は論理値 FALSE を返す可能性がありますが、FALSE として評価される 0 や "" といった値を返す可能性もあります。 詳細については 論理値の セクションを参照してください。この関数の返り値を調べるには ===演算子 を 使用してください。
例 2400. stripos() の例
<?php
$findme = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';
$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);
// いいえ、'a' は明らかに 'xyz' の中には存在しません
if ($pos1 === false) {
echo "The string '$findme' was not found in the string '$mystring1'";
}
// === を使用していることに注意しましょう。単に == としても期待通りに動作
// しません。なぜなら 'a' は 0 番目(最初) の文字だからです。
if ($pos2 !== false) {
echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>
needle が文字列でない場合、それは整数に 変換され、文字列中の位置として扱われます。
オプションのパラメータ offset により、 検索を開始する haystack の位置を指定することが できます。この場合でも返される位置は、 haystack の先頭からの位置のままとなります。
注意: この関数はバイナリデータに対応しています。
strpos()、strrpos()、 strrchr()、substr()、 stristr()、strstr()、 strripos() および str_ireplace() も参照ください。
stripos
19-Jul-2007 04:54
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
?>
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
****************************************/
?>
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.
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;
}
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);
}
}
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 ...
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.
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