note 80551 added to function.str-replace

From: Date: Tue, 22 Jan 2008 04:52:23 +0000
Subject: note 80551 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-135301@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 66.245.0.21 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/80551 Del: integrated -- https://master.php.net/note/delete/80551/integrated Del: useless -- https://master.php.net/note/delete/80551/useless Del: bad code -- https://master.php.net/note/delete/80551/bad+code Del: spam -- https://master.php.net/note/delete/80551/spam Del: non-english -- https://master.php.net/note/delete/80551/non-english Del: in docs -- https://master.php.net/note/delete/80551/in+docs Del: other reasons-- https://master.php.net/note/delete/80551 Reject -- https://master.php.net/note/reject/80551 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135301) next »