RE: [PHP-DB] Re: replace string - case insensitive
| From: | Lars B. Jensen | Date: | Thu, 16 Aug 2001 03:15:25 +0000 |
| Subject: | RE: [PHP-DB] Re: replace string - case insensitive | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-11422@lists.php.net to get a copy of this message | ||
Hugh,
Looking through the code I am not doubting it works, but what I wonder if it
is really a preferred choice over for example reg. expressions ? You have
idea what would be the fastest performancewise choice here ?
/ LJ
-----Original Message-----
From: Hugh Bothwell [mailto:hugh_bothwell@hotmail.com]
Sent: 16. august 2001 12:17
To: php-db@lists.php.net
Subject: Re: [PHP-DB] Re: replace string - case insensitive
"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...
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
For additional commands, e-mail: php-db-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net