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