note 55612 deleted from function.str-ireplace by mazzanet

From: Date: Sat, 17 Sep 2005 11:08:04 +0000
Subject: note 55612 deleted from function.str-ireplace by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-95325@lists.php.net to get a copy of this message
Note Submitter: Jason (support at ejdude dot com) ---- str_ireplace() for PHP <5 <?php // mixed str_ireplace ( mixed search, mixed replace, mixed subject, int &count] ) // Case-insensitive version of str_replace. // If someone knows how to pass the $count paramater as a reference without // requiring it, please let me know. I tried func_get_arg() already. Lines // to do with $count have been commented out by "// >>" if ( !function_exists('str_ireplace') ) { function str_ireplace ($search, $replace, $subject) // >> , &$count) { //Loop back until done if using a subject array if ( is_array($subject) ) { $array_keys = array_keys($subject); while ( true ) { // This is done here rather than in the while() statement because 0 evaluates as false $key = array_pop($array_keys); // If $key is null then there are no more subjects if ( $key === NULL) { return $subject; } $subject[$key] = str_ireplace($search, $replace, $subject[$key]); // >> , $count); } return $subject; } // Loop back until done if using a search array if ( is_array($search) ) { //Only need to check this once $is_array = is_array($replace); while ( true ) { $needle = array_pop($search); // If needle is null then there are no more searches to process if ( $needle === NULL ) { return $subject; } // If no needle, don't even bother // In PHP, empty(0) returns true, so the second check is needed if ( empty($needle) && (string)$needle !== "0" ) { continue; } if ( $is_array == true ) { // If no replacements are left, use empty string instead if ( count($replace) ) { $replacement = array_pop($replace); } else { $replacement = ''; } } else { // If replacement is not an array, use the same value each time $replacement = $replace; } $subject = str_ireplace($needle, $replacement, $subject); // >> , $count); } return $subject; } // Now begins the meat of the function // Define some variables - compiler throws 'E_NOTICE undefined variable' if you don't $newsubject = ''; $end = ''; $offset = 0; // Only need to do this once $lowerneedle = strtolower($search); $lowersubject = strtolower($subject); // Keep going until I say so while ( true ) { // $pos will either be the position of the needle (can be 0) or FALSE $pos = strpos($lowersubject, $lowerneedle, $offset); // Operator === also checks against types, 0 == FALSE, 0 !== FALSE if ($pos === false) { // Tack on the rest $end = substr($subject, $offset); $newsubject .= $end; return $newsubject; } // Get the data before the needle $front = substr($subject, $offset, $pos); // Append the next piece and the replacement $newsubject .= $front . $replace; // Set offset to search beyond needle next time // This is important because the replacement may contain the needle $offset = $pos + strlen($search); // Pretty self explanatory // >> $count++; } // This should never happen, but it's here in case return $subject; } } ?>

« previous php.notes (#95325) next »