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: Tom Buskens <ortega@php.net> | 15 // | Authors: Tom Buskens <ortega@php.net> |
16 // | Aidan Lister <aidan@php.net> | 16 // | Aidan Lister <aidan@php.net> |
17 // +----------------------------------------------------------------------+ 17 // +----------------------------------------------------------------------+
18 // 18 //
19 // $Id: substr_compare.php,v 1.5 2005/01/26 04:55:13 aidan Exp $ 19 // $Id: substr_compare.php,v 1.5 2005/01/26 04:55:13 aidan Exp $
20   20  
21   21  
22 /** 22 /**
23 * Replace substr_compare() 23 * Replace substr_compare()
24 * 24 *
25 * @category PHP 25 * @category PHP
26 * @package PHP_Compat 26 * @package PHP_Compat
27 * @link http://php.net/function.substr_compare 27 * @link http://php.net/function.substr_compare
28 * @author Tom Buskens <ortega@php.net> 28 * @author Tom Buskens <ortega@php.net>
29 * @author Aidan Lister <aidan@php.net> 29 * @author Aidan Lister <aidan@php.net>
30 * @version $Revision: 1.5 $ 30 * @version $Revision: 1.5 $
31 * @since PHP 5 31 * @since PHP 5
32 * @require PHP 4.0.0 (user_error) 32 * @require PHP 4.0.0 (user_error)
33 */ 33 */
34 if (!function_exists('substr_compare')) { 34 if (!function_exists('substr_compare')) {
35 function substr_compare($main_str, $str, $offset, $length = null, $case_insensitive = false) 35 function substr_compare($main_str, $str, $offset, $length = null, $case_insensitive = false)
36 { 36 {
37 if (!is_string($main_str)) { 37 if (!is_string($main_str)) {
38 user_error('substr_compare() expects parameter 1 to be string, ' . 38 user_error('substr_compare() expects parameter 1 to be string, ' .
39 gettype($main_str) . ' given', E_USER_WARNING); 39 gettype($main_str) . ' given', E_USER_WARNING);
40 return; 40 return;
41 } 41 }
42   42  
43 if (!is_string($str)) { 43 if (!is_string($str)) {
44 user_error('substr_compare() expects parameter 2 to be string, ' . 44 user_error('substr_compare() expects parameter 2 to be string, ' .
45 gettype($str) . ' given', E_USER_WARNING); 45 gettype($str) . ' given', E_USER_WARNING);
46 return; 46 return;
47 } 47 }
48 48
49 if (!is_int($offset)) { 49 if (!is_int($offset)) {
50 user_error('substr_compare() expects parameter 3 to be long, ' . 50 user_error('substr_compare() expects parameter 3 to be long, ' .
51 gettype($offset) . ' given', E_USER_WARNING); 51 gettype($offset) . ' given', E_USER_WARNING);
52 return; 52 return;
53 } 53 }
54 54
55 if (is_null($length)) { 55 if (is_null($length)) {
56 $length = strlen($main_str) - $offset; 56 $length = strlen($main_str) - $offset;
57 } elseif ($offset >= strlen($main_str)) { 57 } elseif ($offset >= strlen($main_str)) {
58 user_error('substr_compare() The start position cannot exceed initial string length', 58 user_error('substr_compare() The start position cannot exceed initial string length',
59 E_USER_WARNING); 59 E_USER_WARNING);
60 return false; 60 return false;
61 } 61 }
62   62  
63 $main_str = substr($main_str, $offset, $length); 63 $main_str = substr($main_str, $offset, $length);
64 $str = substr($str, 0, strlen($main_str)); 64 $str = substr($str, 0, strlen($main_str));
65   65  
66 if ($case_insensitive === false) { 66 if ($case_insensitive === false) {
67 return strcmp($main_str, $str); 67 return strcmp($main_str, $str);
68 } else { 68 } else {
69 return strcasecmp($main_str, $str); 69 return strcasecmp($main_str, $str);
70 } 70 }
71 } 71 }
72 } 72 }
73   73  
74 ?> 74 ?>
75 75