Click to See Complete Forum and Search --> : Case insensitive string replace


LordShryku
11-15-2003, 12:35 AM
Sort of a code share/critque here.
This is a small function to perform case-insensitive string replacements.
This is included in PHP 5, but I've seen a couple threads asking this kind of question and being pointed to eregi.
Maybe it's overkill.
Maybe I'm wasting time.
But this is for us eregi-illiterate folk :D

<?php
function str_ireplace($find,$replace,$string) {
/* Create arrays for $find and $replace
If multiple $replace's are present, but
only one $find, it will replace $find
with only the first value in the
$replace array
*/

if(!is_array($find)) {
$find = array($find);
}
if(!is_array($replace)) {
if(!is_array($find)) {
$replace = array($replace);
}
else {
$r = $replace;
unset($replace);
for($i=0;$i<count($find);$i++) {
$replace[$i] = $r;
}
}
}

foreach($find as $key => $value) {
$between = explode(strtolower($value),strtolower($string));
$pos=0;
foreach($between as $betweenKey => $betweenItem) {
$between[$betweenKey] = substr($string,$pos,strlen($betweenItem));
$pos += strlen($betweenItem) + strlen($value);
}
$string = implode($replace[$key],$between);
}
return $string;
}


/* Example */
$text = "foo bar Foo Bar FOo BAr FOO BAR fOo bAR";

/* Given the following... */
$findThis = array("foo","bar");
$useThis = array("blah","BLEH");
$newText = str_ireplace($findThis, $useThis, $text);

/* $newtext will output
"blah BLEH blah BLEH blah BLEH blah BLEH blah BLEH"
*/

Fairly simple. Hope it helps someone.

pyro
11-15-2003, 12:56 AM
Seems like it is totally trying to re-invent the wheel. A case-insensative regex is much less code, and much simpler.

/* Example */
$text = "foo bar Foo Bar FOo BAr FOO BAR fOo bAR";

$text = preg_replace("/foo/i", "blah", $text);
$text = preg_replace("/bar/i", "BLEH", $text);

/* $text will output
"blah BLEH blah BLEH blah BLEH blah BLEH blah BLEH"
*/

Merve
11-15-2003, 12:49 PM
Needs a little more commenting, probably...very good code! Just kind of a waste of time to reinvent the wheel, methinks.

Just one quick question: why do you need to unset $replace?

(No, I'm not hijacking this thread :))

LordShryku
11-15-2003, 01:37 PM
Did that to build a $replace array proportional to $find.

I know this can be done with regex, but I did this for people who don't want to use regex. Doesn't hurt to have choices now does it?

BuzzLY
11-18-2003, 02:14 AM
I can paint a house with a 1 inch brush or a paint sprayer. I don't know many people that would choose to use the 1 inch brush, but I'm sure there's someone out there.

Seems your code is more of an exercise for you rather than a replacement for preg_replace. I doubt there is anyone out there that has learned PHP that would choose to say "no, I don't think I want to learn regex." But as with my previous example, I could be wrong...

Merve
11-19-2003, 09:43 PM
Originally posted by BuzzLY
I can paint a house with a 1 inch brush or a paint sprayer. I don't know many people that would choose to use the 1 inch brush, but I'm sure there's someone out there.

Seems your code is more of an exercise for you rather than a replacement for preg_replace. I doubt there is anyone out there that has learned PHP that would choose to say "no, I don't think I want to learn regex." But as with my previous example, I could be wrong...


True, Buzz. Methinks that some people like the 'adrenaline rush' of painting with a one-inch brush. :D not much of an adrenaline rush, i know, but maybe LShryku got a kick out of coding this. Nonetheless, regex is a lot more powerful...now, if LShryku were to create a LordShrykuRegex, that would be different (throwing ideas at you). And, if he were to bundle it as a LordShryku extension, that would be supercool, but I don't think LordShryku would bother doing that...besides, the code shows good coding skills! This is more of a code share, but it might give someone ideas to make their own functions...and maybe one day there'll be a MerveRegex...i dunno:D The point is that this may be a pointless exercise, but not everything has to have a purpose. Sometime we need to get away from that.

The only critique i have is that maybe you should comment every few lines instead of using one big comment at the top, since the code ain't that long...methinks it'd be a good idea, but wouldn't improve functionality, so why bother?

Weedpacket
11-19-2003, 10:49 PM
A project for someone with way too much time on their hands:

MiniPHP

It's a chopped down version of PHP with the minimum of functionality required to implement all of it - all of PHP's functions are to be implemented, but as many as possible are to be implemented in PHP and included.

Moonglobe
11-19-2003, 11:08 PM
Originally posted by Weedpacket
A project for someone with way too much time on their hands: hey! that sounds like me.............


and that does sound like fun......... :D