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 // +----------------------------------------------------------------------+ 16 // +----------------------------------------------------------------------+
17 // 17 //
18 // $Id: str_ireplace.php,v 1.18 2005/01/26 04:55:13 aidan Exp $ 18 // $Id: str_ireplace.php,v 1.18 2005/01/26 04:55:13 aidan Exp $
19   19  
20   20  
21 /** 21 /**
22 * Replace str_ireplace() 22 * Replace str_ireplace()
23 * 23 *
24 * @category PHP 24 * @category PHP
25 * @package PHP_Compat 25 * @package PHP_Compat
26 * @link http://php.net/function.str_ireplace 26 * @link http://php.net/function.str_ireplace
27 * @author Aidan Lister <aidan@php.net> 27 * @author Aidan Lister <aidan@php.net>
28 * @version $Revision: 1.18 $ 28 * @version $Revision: 1.18 $
29 * @since PHP 5 29 * @since PHP 5
30 * @require PHP 4.0.0 (user_error) 30 * @require PHP 4.0.0 (user_error)
31 * @note count not by returned by reference, to enable 31 * @note count not by returned by reference, to enable
32 * change '$count = null' to '&$count' 32 * change '$count = null' to '&$count'
33 */ 33 */
34 if (!function_exists('str_ireplace')) { 34 if (!function_exists('str_ireplace')) {
35 function str_ireplace($search, $replace, $subject, $count = null) 35 function str_ireplace($search, $replace, $subject, $count = null)
36 { 36 {
37 // Sanity check 37 // Sanity check
38 if (is_string($search) && is_array($replace)) { 38 if (is_string($search) && is_array($replace)) {
39 user_error('Array to string conversion', E_USER_NOTICE); 39 user_error('Array to string conversion', E_USER_NOTICE);
40 $replace = (string) $replace; 40 $replace = (string) $replace;
41 } 41 }
42   42  
43 // If search isn't an array, make it one 43 // If search isn't an array, make it one
44 if (!is_array($search)) { 44 if (!is_array($search)) {
45 $search = array ($search); 45 $search = array ($search);
46 } 46 }
47 $search = array_values($search); 47 $search = array_values($search);
48   48  
49 // If replace isn't an array, make it one, and pad it to the length of search 49 // If replace isn't an array, make it one, and pad it to the length of search
50 if (!is_array($replace)) { 50 if (!is_array($replace)) {
51 $replace_string = $replace; 51 $replace_string = $replace;
52   52  
53 $replace = array (); 53 $replace = array ();
54 for ($i = 0, $c = count($search); $i < $c; $i++) { 54 for ($i = 0, $c = count($search); $i < $c; $i++) {
55 $replace[$i] = $replace_string; 55 $replace[$i] = $replace_string;
56 } 56 }
57 } 57 }
58 $replace = array_values($replace); 58 $replace = array_values($replace);
59   59  
60 // Check the replace array is padded to the correct length 60 // Check the replace array is padded to the correct length
61 $length_replace = count($replace); 61 $length_replace = count($replace);
62 $length_search = count($search); 62 $length_search = count($search);
63 if ($length_replace < $length_search) { 63 if ($length_replace < $length_search) {
64 for ($i = $length_replace; $i < $length_search; $i++) { 64 for ($i = $length_replace; $i < $length_search; $i++) {
65 $replace[$i] = ''; 65 $replace[$i] = '';
66 } 66 }
67 } 67 }
68   68  
69 // If subject is not an array, make it one 69 // If subject is not an array, make it one
70 $was_array = false; 70 $was_array = false;
71 if (!is_array($subject)) { 71 if (!is_array($subject)) {
72 $was_array = true; 72 $was_array = true;
73 $subject = array ($subject); 73 $subject = array ($subject);
74 } 74 }
75   75  
76 // Loop through each subject 76 // Loop through each subject
77 $count = 0; 77 $count = 0;
78 foreach ($subject as $subject_key => $subject_value) { 78 foreach ($subject as $subject_key => $subject_value) {
79 // Loop through each search 79 // Loop through each search
80 foreach ($search as $search_key => $search_value) { 80 foreach ($search as $search_key => $search_value) {
81 // Split the array into segments, in between each part is our search 81 // Split the array into segments, in between each part is our search
82 $segments = explode(strtolower($search_value), strtolower($subject_value)); 82 $segments = explode(strtolower($search_value), strtolower($subject_value));
83   83  
84 // The number of replacements done is the number of segments minus the first 84 // The number of replacements done is the number of segments minus the first
85 $count += count($segments) - 1; 85 $count += count($segments) - 1;
86 $pos = 0; 86 $pos = 0;
87   87  
88 // Loop through each segment 88 // Loop through each segment
89 foreach ($segments as $segment_key => $segment_value) { 89 foreach ($segments as $segment_key => $segment_value) {
90 // Replace the lowercase segments with the upper case versions 90 // Replace the lowercase segments with the upper case versions
91 $segments[$segment_key] = substr($subject_value, $pos, strlen($segment_value)); 91 $segments[$segment_key] = substr($subject_value, $pos, strlen($segment_value));
92 // Increase the position relative to the initial string 92 // Increase the position relative to the initial string
93 $pos += strlen($segment_value) + strlen($search_value); 93 $pos += strlen($segment_value) + strlen($search_value);
94 } 94 }
95   95  
96 // Put our original string back together 96 // Put our original string back together
97 $subject_value = implode($replace[$search_key], $segments); 97 $subject_value = implode($replace[$search_key], $segments);
98 } 98 }
99   99  
100 $result[$subject_key] = $subject_value; 100 $result[$subject_key] = $subject_value;
101 } 101 }
102   102  
103 // Check if subject was initially a string and return it as a string 103 // Check if subject was initially a string and return it as a string
104 if ($was_array === true) { 104 if ($was_array === true) {
105 return $result[0]; 105 return $result[0];
106 } 106 }
107   107  
108 // Otherwise, just return the array 108 // Otherwise, just return the array
109 return $result; 109 return $result;
110 } 110 }
111 } 111 }
112   112  
113 ?> 113 ?>
114 114