note 109365 deleted from function.str-replace by cmb

From: Date: Sun, 15 Sep 2019 14:48:06 +0000
Subject: note 109365 deleted from function.str-replace by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212211@lists.php.net to get a copy of this message
Note Submitter: bfrohs at gmail dot com ---- This function implements $limit for str_replace(); is 100% backward compatible with str_replace(); uses str_replace() whenever possible (for performance reasons); supports arrays for $search, $replace, and $subject; and is fully commented. <?php /** * Replace $limit occurences of the search string with the replacement * @param mixed $search The value being searched for. An array may be used to * designate multiple needles. * @param mixed $replace The replacement value that replaces found search * values. An array may be used to designate multiple replacements. * @param mixed $subject The string or array being searched and replaced on. If * subject is an array, then the search and replace is performed with every * entry of subject, and the return value is an array as well. * @param string $count If passed, this will be set to the number of * replacements performed. * @param int $limit The maximum possible replacements for each pattern in each * subject string. Defaults to -1 (no limit). * @return string This function returns a string with the replaced values. */ function str_replace_limit($search, $replace, $subject, &$count, $limit = -1){ $count = 0; // Invalid $limit provided if(!($limit===strval(intval(strval($limit))))){ trigger_error('Invalid $limit '.$limit.' provided. Expecting an '. 'integer', E_USER_WARNING); return $subject; } // Invalid $limit provided if($limit<-1){ trigger_error('Invalid $limit '.$limit.' provided. Expecting -1 or '. 'a positive integer', E_USER_WARNING); return $subject; } // No replacements necessary if($limit===0){ trigger_error('Invalid $limit '.$limit.' provided. Expecting -1 or '. 'a positive integer', E_USER_NOTICE); return $subject; } // Use str_replace() when possible if($limit===-1){ return str_replace($search, $replace, $subject, $count); } if(is_array($subject)){ // Loop through $subject values foreach($subject as $key => $this_subject){ // Skip values that are arrays if(!is_array($this_subject)){ // Call this function again $this_function = __FUNCTION__; $subject[$key] = $this_function($search, $replace, $this_subject, $this_count, $limit); // Adjust $count $count += $this_count; // Adjust $limit if($limit!=-1){ $limit -= $this_count; } // Reached $limit if($limit===0){ return $subject; } } } return $subject; } elseif(is_array($search)){ // Clear keys of $search $search = array_values($search); // Clear keys of $replace if(is_array($replace)){ $replace = array_values($replace); } // Loop through $search foreach($search as $key => $this_search){ // Don't support multi-dimensional arrays $this_search = strval($this_search); // If $replace is an array, use $replace[$key] if exists, else '' if(is_array($replace)){ if(array_key_exists($key, $replace)){ $this_replace = strval($replace[$key]); } else { $this_replace = ''; } } else { $this_replace = strval($replace); } // Call this function again for $this_function = __FUNCTION__; $subject = $this_function($this_search, $this_replace, $subject, $this_count, $limit); // Adjust $count $count += $this_count; // Adjust $limit if($limit!=-1){ $limit -= $this_count; } // Reached $limit if($limit===0){ return $subject; } } return $subject; } else { $search = strval($search); $replace = strval($replace); // Get position of first $search $pos = strpos($subject, $search); // Return $subject if $search cannot be found if($pos===false){ return $subject; } // Get length of $search $search_len = strlen($search); // Loop until $search cannot be found or $limit is reached for($i=0;(($i<$limit)||($limit===-1));$i++){ $subject = substr_replace($subject, $replace, $pos, $search_len); // Increase $count $count++; // Get location of next $search $pos = strpos($subject, $search); // Break out of loop if($pos===false){ break; } } return $subject; } } ?>

« previous php.notes (#212211) next »