|
strripos
Find position of last occurrence of a case-insensitive string in a string
(PHP 5)
Example 2464. A simple strripos() example<?php The above example will output: Congratulations! Code Examples / Notes » strriposaidan
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 electrofox
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. yanik lupien
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); } } ?> peev dot alexander
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*/ ?> electrofox
Actually, the above, "Simple way to implement this function in PHP 4" by Yanik Lupien, should be: <?php if (function_exists('strripos') == false) { function strripos($haystack, $needle) { return strlen($haystack) - strpos(strrev($haystack), strrev($needle)); } } ?> Note the reversal (<?php strrev($needle)?>) of the search string. This was left out in Yanik's example, and without it, you'll simply get the length of the haystack, as the forward string will not likely be found in the reversed haystack. Thus; if we reverse the haystack, any instance of the search string ($needle) therein will also be reversed, so we must reverse it to look for it. :) |
Change Languageaddcslashes 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 html_entity_decode htmlentities htmlspecialchars_decode htmlspecialchars implode join levenshtein localeconv ltrim md5_file md5 metaphone money_format nl_langinfo nl2br number_format ord parse_str printf quoted_printable_decode 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 |