Re: Re: replace string - case insensitive

From: Date: Thu, 16 Aug 2001 03:17:18 +0000
Subject: Re: Re: replace string - case insensitive
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-11421@lists.php.net to get a copy of this message
"Lars B. Jensen" <ljweb@ljweb.com> wrote in message news:PMEIKBLAHLHGGJANAICMIELLCEAA.ljweb@ljweb.com... > > depending on what you're replacing with what, a solution is eregi_replace. I > do not advice to upper nor lowercase the source as it will change parts of > data unwantedly. Yes, I agree; another solution is as follows: function alt_str_replace($search, $replace, $subject) { // Temporary copies of search string and subject string; // both shifted to lowercase to make the search case insensitive. $t_search = strtolower($search); $t_subject = strtolower($subject); // Initialize result string $res = ""; // Initialize search vars $search_offs = 0; $shift = strlen($search); // As long as another match is found... while (($t = strpos($t_subject, $t_search, $search_offs)) !== false) { // Copy non-matching portion of string $res .= substr($subject, $search_offs, $t-$search_offs); // Add replacement value $res .= $replace; // Update search offset $search_offs = $t + $shift; } // Add trailing end of string $res .= substr($subject, $search_offs); return $res; } Hope that helps...

« previous php.db (#11421) next »