note 42518 modified in function.str-replace by pollita

From: Date: Wed, 19 May 2004 22:58:37 +0000
Subject: note 42518 modified in function.str-replace by pollita
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-69853@lists.php.net to get a copy of this message
Function str_replace_clean() /* * The basis for this function comes from the contribution by Xavier here above. * I modified it so that all parameters can be arrays or strings, * and I added an option for case-sensitive comparison or not (standard * function str_replace() compares case-sensitive). */ public function str_replace_clean($search_in, $replace_in, $subject_in, $cs=true, $subject_out=array()) { if ($cs !== true and $cs !== false) return $subject_in; $search = $search_text = $replace = $replace_text = array(); if (is_array($search_in)) { $search = $search_in; } elseif (is_string($search_in)) { $search[] = $search_in; //array_push doesn't work here } if (is_array($replace_in)) { $replace = $replace_in; } elseif (is_string($replace_in)) { //make an array with the length of array $search foreach ($search as $dummy) { $replace[] = $replace_in; //array_push doesn't work here } } if (is_array($subject_in)) { foreach ($subject_in as $subject) { $resultsubject = $this->do_while_for_str_replace_clean($search, $replace, $subject, $cs, $subject_out); $subject_out[] = $resultsubject; //array_push doesn't work here } return $subject_out; } elseif (is_string($subject_in)) { $subject=$subject_in; $resultsubject = $this->do_while_for_str_replace_clean($search, $replace, $subject, $cs, $subject_out); return $resultsubject; } } /* * Private function do_while_for_str_replace_clean() * See public function str_replace_clean() */ private function do_while_for_str_replace_clean($search, $replace, $subject, $cs, $subject_out) { while (count($search)) { // get current terms, reduce the arrays for the next iteration $search_text = array_shift($search); $replace_text = array_shift($replace); // check if the substring is present if ($cs === true) { $pos = strpos($subject, $search_text); $break = $search_text; } else { $pos = strpos(strtolower($subject), strtolower($search_text)); if (is_int($pos)) { //replace all matches with their lowercase values for later explode() $len=strlen($search_text); $subject_len = strlen($subject); for ($pos2 = $pos; ($pos2+$len) <= $subject_len ; $pos2 = $newpos) { $subject = substr($subject,0,$pos2) . strtolower($search_text) . substr($subject,$pos2+$len); $newpos = strpos(strtolower($subject), strtolower($search_text),$pos2+$len); if (!is_int($newpos)) break; } } $break=strtolower($search_text); } if (is_int($pos)) { // match found - break in pieces $pieces = explode($break, $subject); if (count($search)) // only if there are more substitutions to do { // make next substitutions in every piece of text between matches foreach ($pieces as $k => $v) { if (strlen($v)) $pieces[$k] = $this->str_replace_clean($search, $replace, $v, $cs, $subject_out); } } $subject = join($replace_text, $pieces); break; } } //end while return $subject; } -------------------------------------------------------------------- The functions are written in an object-oriënted style, and tested with PHP5 (Release Candidate 2). If you do not want to use these functions as methods in a class, remove all " $this-> " inside the functions, and remove "public" and "private" before "function". --was-- Function str_replace_clean() /* * The basis for this function comes from the contribution by Xavier here above. * I modified it so that all parameters can be arrays or strings, * and I added an option for case-sensitive comparison or not (standard * function str_replace() compares case-sensitive). */ public function str_replace_clean($search_in, $replace_in, $subject_in, $cs=true, $subject_out=array()) { if ($cs!true and $cs!false) return $subject_in; $search=$search_text=$replace=$replace_text=array(); if (is_array($search_in)) { $search = $search_in; } elseif (is_string($search_in)) { $search![] = $search_in; //array_push doesn't work here } if (is_array($replace_in)) { $replace = $replace_in; } elseif (is_string($replace_in)) { //make an array with the length of array $search foreach ($search as $dummy) { $replace![] = $replace_in; //array_push doesn't work here } } if (is_array($subject_in)) { foreach ($subject_in as $subject) { $resultsubject = $this->do_while_for_str_replace_clean($search, $replace, $subject, $cs, $subject_out); $subject_out![] = $resultsubject; //array_push doesn't work here } return $subject_out; } elseif (is_string($subject_in)) { $subject=$subject_in; $resultsubject = $this->do_while_for_str_replace_clean($search, $replace, $subject, $cs, $subject_out); return $resultsubject; } } /* * Private function do_while_for_str_replace_clean() * See public function str_replace_clean() */ private function do_while_for_str_replace_clean($search, $replace, $subject, $cs, $subject_out) { while (count($search)) { // get current terms, reduce the arrays for the next iteration $search_text = array_shift($search); $replace_text = array_shift($replace); // check if the substring is present if ($cs===true) { $pos = strpos($subject, $search_text); $break=$search_text; } else { $pos = strpos(strtolower($subject), strtolower($search_text)); if (is_int($pos)) { //replace all matches with their lowercase values for later explode() $len=strlen($search_text); $subject_len=strlen($subject); for ($pos2=$pos; ($pos2+$len)<=$subject_len ; $pos2=$newpos) { $subject=substr($subject,0,$pos2) .strtolower($search_text) .substr($subject,$pos2+$len); $newpos = strpos(strtolower($subject), strtolower($search_text),$pos2+$len); if (!is_int($newpos)) break; } } $break=strtolower($search_text); } if (is_int($pos)) { // match found - break in pieces $pieces = explode($break, $subject); if (count($search)) // only if there are more substitutions to do { // make next substitutions in every piece of text between matches foreach ($pieces as $k => $v) { if (strlen($v)) $pieces[$k] = $this->str_replace_clean($search, $replace, $v, $cs, $subject_out); } } $subject = join($replace_text, $pieces); break; } } //end while return $subject; } -------------------------------------------------------------------- The functions are written in an object-oriënted style, and tested with PHP5 (Release Candidate 2). If you do not want to use these functions as methods in a class, remove all " $this-> " inside the functions, and remove "public" and "private" before "function". http://www.php.net/manual/en/function.str-replace.php

« previous php.notes (#69853) next »