Rev 130 Rev 139
Line 1... Line 1...
1 <?php 1 <?php
2 // +----------------------------------------------------------------------+ 2 // +----------------------------------------------------------------------+
3 // | PHP Version 4 | 3 // | PHP Version 4 |
4 // +----------------------------------------------------------------------+ 4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2004 The PHP Group | 5 // | Copyright (c) 1997-2004 The PHP Group |
6 // +----------------------------------------------------------------------+ 6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 3.0 of the PHP license, | 7 // | This source file is subject to version 3.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is | 8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at | 9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/3_0.txt. | 10 // | http://www.php.net/license/3_0.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to | 11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to | 12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. | 13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+ 14 // +----------------------------------------------------------------------+
15 // | Authors: Aidan Lister <aidan@php.net> | 15 // | Authors: Aidan Lister <aidan@php.net> |
16 // | Stephan Schmidt <schst@php.net> | 16 // | Stephan Schmidt <schst@php.net> |
17 // +----------------------------------------------------------------------+ 17 // +----------------------------------------------------------------------+
18 // 18 //
19 // $Id: strripos.php,v 1.24 2005/08/10 10:19:59 aidan Exp $ 19 // $Id: strripos.php,v 1.24 2005/08/10 10:19:59 aidan Exp $
20   20  
21   21  
22 /** 22 /**
23 * Replace strripos() 23 * Replace strripos()
24 * 24 *
25 * @category PHP 25 * @category PHP
26 * @package PHP_Compat 26 * @package PHP_Compat
27 * @link http://php.net/function.strripos 27 * @link http://php.net/function.strripos
28 * @author Aidan Lister <aidan@php.net> 28 * @author Aidan Lister <aidan@php.net>
29 * @version $Revision: 1.24 $ 29 * @version $Revision: 1.24 $
30 * @since PHP 5 30 * @since PHP 5
31 * @require PHP 4.0.0 (user_error) 31 * @require PHP 4.0.0 (user_error)
32 */ 32 */
33 if (!function_exists('strripos')) { 33 if (!function_exists('strripos')) {
34 function strripos($haystack, $needle, $offset = null) 34 function strripos($haystack, $needle, $offset = null)
35 { 35 {
36 // Sanity check 36 // Sanity check
37 if (!is_scalar($haystack)) { 37 if (!is_scalar($haystack)) {
38 user_error('strripos() expects parameter 1 to be scalar, ' . 38 user_error('strripos() expects parameter 1 to be scalar, ' .
39 gettype($haystack) . ' given', E_USER_WARNING); 39 gettype($haystack) . ' given', E_USER_WARNING);
40 return false; 40 return false;
41 } 41 }
42   42  
43 if (!is_scalar($needle)) { 43 if (!is_scalar($needle)) {
44 user_error('strripos() expects parameter 2 to be scalar, ' . 44 user_error('strripos() expects parameter 2 to be scalar, ' .
45 gettype($needle) . ' given', E_USER_WARNING); 45 gettype($needle) . ' given', E_USER_WARNING);
46 return false; 46 return false;
47 } 47 }
48   48  
49 if (!is_int($offset) && !is_bool($offset) && !is_null($offset)) { 49 if (!is_int($offset) && !is_bool($offset) && !is_null($offset)) {
50 user_error('strripos() expects parameter 3 to be long, ' . 50 user_error('strripos() expects parameter 3 to be long, ' .
51 gettype($offset) . ' given', E_USER_WARNING); 51 gettype($offset) . ' given', E_USER_WARNING);
52 return false; 52 return false;
53 } 53 }
54   54  
55 // Initialise variables 55 // Initialise variables
56 $needle = strtolower($needle); 56 $needle = strtolower($needle);
57 $haystack = strtolower($haystack); 57 $haystack = strtolower($haystack);
58 $needle_fc = $needle{0}; 58 $needle_fc = $needle{0};
59 $needle_len = strlen($needle); 59 $needle_len = strlen($needle);
60 $haystack_len = strlen($haystack); 60 $haystack_len = strlen($haystack);
61 $offset = (int) $offset; 61 $offset = (int) $offset;
62 $leftlimit = ($offset >= 0) ? $offset : 0; 62 $leftlimit = ($offset >= 0) ? $offset : 0;
63 $p = ($offset >= 0) ? 63 $p = ($offset >= 0) ?
64 $haystack_len : 64 $haystack_len :
65 $haystack_len + $offset + 1; 65 $haystack_len + $offset + 1;
66   66  
67 // Reverse iterate haystack 67 // Reverse iterate haystack
68 while (--$p >= $leftlimit) { 68 while (--$p >= $leftlimit) {
69 if ($needle_fc === $haystack{$p} && 69 if ($needle_fc === $haystack{$p} &&
70 substr($haystack, $p, $needle_len) === $needle) { 70 substr($haystack, $p, $needle_len) === $needle) {
71 return $p; 71 return $p;
72 } 72 }
73 } 73 }
74   74  
75 return false; 75 return false;
76 } 76 }
77 } 77 }
78   78  
79 ?> 79 ?>
80 80