note 80551 deleted from function.str-replace by felipe

From: Date: Sun, 23 Mar 2008 23:07:35 +0000
Subject: note 80551 deleted from function.str-replace by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137433@lists.php.net to get a copy of this message
Note Submitter: steve at psychsurveys dot org ---- I wrote a replace function, str_cased_replace() that does its best to match the case of replacement strings to the case of the target strings. For instance, if the target string "Your Name" is to be replaced with the name "William James": (target case) ORIGINAL becomes REPLACEMENT ---------------------------------------------------------------- (lowercase) your name --> william james (uppercase) YOUR NAME --> WILLIAM JAMES (title case) Your Name --> William James (sentence case) Your name --> William james Despite the fact that the case is maintained (when possible) by the replacement strings, the function itself performs a case insensitive search for target strings. That is, the case of the $searcharray and $replacearray arguments is irrelevant. The only exception is when the target string does not appear to be lowercase, uppercase, title case, or sentence case. Then the replacement will be cased the same way it is in the $replacearray argument. <?php str_cased_replace(array("fruit1","fruit2"), array("apples","oranges"), "Fruit1 and fruit2 ought not be compared!"); //results in "Apples and oranges ought not be compared!" ?> <?php function str_cased_replace($searcharray, $replacearray, $subject){ //CONVERT SEARCH STRING INTO SINGLE ELEMENT SEARCH ARRAY if (is_string($searcharray)) { $temp = $searcharray; unset($searcharray); $searcharray[] = $temp; } //LOOP THROUGH ELEMENTS IN SEARCH ARRAY foreach ($searcharray as $index => $search) { //IF REPLACE ARRAY IS A STRING, USE IT AS THE REPLACEMENT if (is_string($replacearray)) { $replace = $replacearray; //IF REPLACE ARRAY HAS AN ELEMENT AT THE SAME INDEX AS THE SEARCH ARRAY ELEMENT, USE IT } elseif (isset($replacearray[$index])) { $replace = $replacearray[$index]; //IF REPLACE ARRAY IS SHORTER THAN SEARCH ARRAY, REPLACE WITH EMPTY STRING } else { $replace = ""; } $offset = 0; //REPLACE EACH INSTANCE OF SEARCH FROM THE BEGINNING TO THE END OF SUBJECT while (stripos($subject, $search, $offset) !== false) { $searchpos = stripos($subject, $search, $offset); $searchlen = strlen($search); $replacelen = strlen($replace); $before = substr($subject, 0, $searchpos); $target = substr($subject, $searchpos, $searchlen); $after = substr($subject, ($searchpos + $searchlen)); //REPLACE SEARCH STRING WITH CASE MATCHED REPLACEMENT if (strcmp(strtolower($target),$target) == 0) { $replacement = strtolower($replace); } else if (strcmp(strtoupper($target),$target) == 0) { $replacement = strtoupper($replace); } else if (strcmp(ucwords($target),$target) == 0) { $replacement = ucwords($replace); } else if (strcmp(ucfirst($target),$target) == 0) { $replacement = ucfirst($replace); } else { $replacement = $replace; } $offset = strlen($before . $replacement); $subject = $before . $replacement . $after; $replacement = ''; } } return $subject; } ?>

« previous php.notes (#137433) next »