| // +----------------------------------------------------------------------+ // // $Id: str_ireplace.php,v 1.1 2004-10-26 18:22:16 kostja Exp $ // /** * Replace str_ireplace() * * @category PHP * @package PHP_Compat * @link http://php.net/function.str_ireplace * @author Aidan Lister * @version $Revision: 1.1 $ * @since PHP 5 * @internal count not by returned by reference - not possible in php4 * @require PHP 4.0.1 (trigger_error) */ if (!function_exists('str_ireplace')) { function str_ireplace ($search, $replace, $subject, $count = null) { if (is_string($search) && is_array($replace)) { trigger_error('Array to string conversion', E_USER_NOTICE); $replace = (string) $replace; } // If search isn't an array, make it one if (!is_array($search)) { $search = array ($search); } // If replace isn't an array, make it one, and pad it to the length of search if (!is_array($replace)) { $replace_string = $replace; $replace = array (); for ($i = 0, $c = count($search); $i < $c; $i++) { $replace[$i] = $replace_string; } } // Check the replace array is padded to the correct length $length_replace = count($replace); $length_search = count($search); if ($length_replace < $length_search) { for ($i = $length_replace; $i < $length_search; $i++) { $replace[$i] = ''; } } // If subject is not an array, make it one $was_array = false; if (!is_array($subject)) { $was_array = true; $subject = array ($subject); } // Loop through each subject $count = 0; foreach ($subject as $subject_key => $subject_value) { // Loop through each search foreach ($search as $search_key => $search_value) { // Split the array into segments, in between each part is our search $segments = explode(strtolower($search_value), strtolower($subject_value)); // The number of replacements done is the number of segments minus the first $count += count($segments) - 1; $pos = 0; // Loop through each segment foreach ($segments as $segment_key => $segment_value) { // Replace the lowercase segments with the upper case versions $segments[$segment_key] = substr($subject_value, $pos, strlen($segment_value)); // Increase the position relative to the initial string $pos += strlen($segment_value) + strlen($search_value); } // Put our original string back together $subject_value = implode($replace[$search_key], $segments); } $result[$subject_key] = $subject_value; } // Check if subject was initially a string and return it as a string if ($was_array === true) { return $result[0]; } // Otherwise, just return the array return $result; } } ?>