strripos
(PHP 5)
strripos — Find the position of the last occurrence of a case-insensitive substring in a string
Description
$haystack
, string $needle
[, int $offset
= 0
] )
Find the numeric position of the last occurrence of
needle
in the haystack
string.
Unlike the strrpos(), strripos() is case-insensitive.
Parameters
-
haystack
-
The string to search in.
-
needle
-
If
needle
is not a string, it is converted to an integer and applied as the ordinal value of a character. -
offset
-
If specified, search will start this number of characters counted from the beginning of the string. If the value is negative, search will instead start from that many characters from the end of the string, searching backwards.
Return Values
Returns the position where the needle exists relative to the beginnning of
the haystack
string (independent of search direction
or offset).
Also note that string positions start at 0, and not 1.
Returns FALSE
if the needle was not found.
This function may
return Boolean FALSE
, but may also return a non-Boolean value which
evaluates to FALSE
. Please read the section on Booleans for more
information. Use the ===
operator for testing the return value of this
function.
Examples
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)";
}
?>
The above example will output:
Congratulations! We found the last (aB) in (ababcd) at position (2)
See Also
- strpos() - Find the position of the first occurrence of a substring in a string
- stripos() - Find the position of the first occurrence of a case-insensitive substring in a string
- strrpos() - Find the position of the last occurrence of a substring in a string
- strrchr() - Find the last occurrence of a character in a string
- stristr() - Case-insensitive strstr
- substr() - Return part of a string
- addcslashes
- addslashes
- bin2hex
- chop
- chr
- chunk_split
- convert_cyr_string
- convert_uudecode
- convert_uuencode
- count_chars
- crc32
- crypt
- echo
- explode
- fprintf
- get_html_translation_table
- hebrev
- hebrevc
- hex2bin
- html_entity_decode
- htmlentities
- htmlspecialchars_decode
- htmlspecialchars
- implode
- join
- lcfirst
- levenshtein
- localeconv
- ltrim
- md5_file
- md5
- metaphone
- money_format
- nl_langinfo
- nl2br
- number_format
- ord
- parse_str
- printf
- quoted_printable_decode
- quoted_printable_encode
- quotemeta
- rtrim
- setlocale
- sha1_file
- sha1
- similar_text
- soundex
- sprintf
- sscanf
- str_getcsv
- str_ireplace
- str_pad
- str_repeat
- str_replace
- str_rot13
- str_shuffle
- str_split
- str_word_count
- strcasecmp
- strchr
- strcmp
- strcoll
- strcspn
- strip_tags
- stripcslashes
- stripos
- stripslashes
- stristr
- strlen
- strnatcasecmp
- strnatcmp
- strncasecmp
- strncmp
- strpbrk
- strpos
- strrchr
- strrev
- strripos
- strrpos
- strspn
- strstr
- strtok
- strtolower
- strtoupper
- strtr
- substr_compare
- substr_count
- substr_replace
- substr
- trim
- ucfirst
- ucwords
- vfprintf
- vprintf
- vsprintf
- wordwrap
Коментарии
Simple way to implement this function in PHP 4
<?php
if (function_exists('strripos') == false) {
function strripos($haystack, $needle) {
return strlen($haystack) - strpos(strrev($haystack), $needle);
}
}
?>
Sorry, I made that last post a bit prematurely. One more thing wrong with the simple php4 version is that it breaks if the string is not found. It should actually look like this:
<?php
if (function_exists('strripos') == false) {
function strripos($haystack, $needle) {
$pos = strlen($haystack) - strpos(strrev($haystack), strrev($needle));
if ($pos == strlen($haystack)) { $pos = 0; }
return $pos;
}
}
?>
Note, we now check to see if the $needle was found, and if it isn't, we return 0.
I think you shouldn't underestimate the length of $needle in the search of THE FIRST POSITION of it's last occurrence in the string. I improved the posted function, with added support for offset. I think this is an exact copy of the real function:
<?php
if(!function_exists("strripos")){
function strripos($haystack, $needle, $offset=0) {
if($offset<0){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, $offset ) );
}
$pos = strlen($haystack) - (strpos($temp_cut, strrev($needle)) + $offset + strlen($needle));
if ($pos == strlen($haystack)) { $pos = 0; }
return $pos;
}/* endfunction strripos*/
}/* endfunction exists strripos*/
?>
OK, I guess this will be the final function implementation for PHP 4.x versions ( my previous posts are invalid )
<?php
if(!function_exists("stripos")){
function stripos( $str, $needle, $offset = 0 ){
return strpos( strtolower( $str ), strtolower( $needle ), $offset );
}/* endfunction stripos */
}/* endfunction exists stripos */
if(!function_exists("strripos")){
function strripos( $haystack, $needle, $offset = 0 ) {
if( !is_string( $needle ) )$needle = chr( intval( $needle ) );
if( $offset < 0 ){
$temp_cut = strrev( substr( $haystack, 0, abs($offset) ) );
}
else{
$temp_cut = strrev( substr( $haystack, 0, max( ( strlen($haystack) - $offset ), 0 ) ) );
}
if( ( $found = stripos( $temp_cut, strrev($needle) ) ) === FALSE )return FALSE;
$pos = ( strlen( $haystack ) - ( $found + $offset + strlen( $needle ) ) );
return $pos;
}/* endfunction strripos */
}/* endfunction exists strripos */
?>
Suppose you just need a stripos function working backwards expecting that strripos does this job, you better use the following code of a custom function named strbipos:
<?php
function strbipos($haystack="", $needle="", $offset=0) {
// Search backwards in $haystack for $needle starting from $offset and return the position found or false
$len = strlen($haystack);
$pos = stripos(strrev($haystack), strrev($needle), $len - $offset - 1);
return ( ($pos === false) ? false : $len - strlen($needle) - $pos );
}
// Test
$body = "01234Xy7890XYz456xy90";
$str = "xY";
$len = strlen($body);
echo "TEST POSITIVE offset VALUES IN strbipos<br>";
for ($i = 0; $i < $len; $i++) {
echo "Search in [$body] for [$str] starting from offset [$i]: [" . strbipos($body, $str, $i) . "]<br>";
}
?>
Note that this function does exactly what it says and its results are different comparing to PHP 5 strripos function.
Generally speaking, linear searches are from start to end, not end to start - which makes sense from a human perspective. If you need to find strings in a string backwards, reverse your haystack and needle rather than manually chopping it up.