search for in the  
<strrevstrrpos>
Last updated: Thu, 19 May 2005

strripos

(PHP 5)

strripos --  Find position of last occurrence of a case-insensitive string in a string

Description

int strripos ( string haystack, string needle [, int offset] )

Returns the numeric position of the last occurrence of needle in the haystack string. Unlike strrpos(), strripos() is case-insensitive. Also note that string positions start at 0, and not 1.

Note that the needle may be a string of one or more characters.

If needle is not found, FALSE is returned.

Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Example 1. A simple strripos() example

<?php
$haystack
= 'ababcd';
$needle  = 'aB';

$pos      = strripos($haystack, $needle);

if (
$pos === false) {
   echo
"Sorry, we did not find ($needle) in ($haystack)";
} else {
   echo
"Congratulations!\n";
   echo
"We found the last ($needle) in ($haystack) at position ($pos)";
}
?>

Outputs:

Congratulations!
   We found the last (aB) in (ababcd) at position (2)

offset may be specified to begin searching an arbitrary number of characters into the string. Negative values will stop searching at an arbitrary point prior to the end of the string.

See also strrpos(), strrchr(), substr(), stripos() and stristr().



User Contributed Notes
strripos
michael edmonson
08-Apr-2005 05:50
For php 4 users I found this much simpler than the above

function lastPos($string, $needle){
   $len=strlen($string);

   for ($i=$len-1; $i>-1;$i--){
       if (substr($string, $i, 1)==$needle) return ($i);
          
   }

   return FALSE;
}
nbenitez at cenobioracing dot com
23-Mar-2005 06:47
Because this function exists only in PHP5, I provide here a strripos() compatible function for PHP4 (except for the *offset* parameter that I never use):

<?php

function stripos($haystack, $needle, $offset = 0) {
  
// first we need also the php4 compatible function for stripos()
  
return strpos(strtolower($haystack), strtolower($needle),$offset);
}

function
strripos($haystack, $needle) {
  
$iter = stripos($haystack, $needle);
  
$pos = -1;
   while (
$iter !== false) {
      
$pos = $iter + ($pos+1);
      
$iter = stripos(substr($haystack,$pos+1), $needle);
     }
     return (
$pos != -1) ? $pos : false;
}

?>
aidan at php dot net
30-May-2004 12: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

<strrevstrrpos>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT