To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-15-2003, 12:35 AM   #1
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
Case insensitive string replace

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

PHP Code:
<?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;
}
PHP Code:
/* 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.
LordShryku is offline   Reply With Quote
Old 11-15-2003, 12:56 AM   #2
pyro
W3C &amp;&amp; PHP
 
pyro's Avatar
 
Join Date: Dec 2002
Posts: 615
Seems like it is totally trying to re-invent the wheel. A case-insensative regex is much less code, and much simpler.

PHP Code:
/* 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"
*/
__________________

Personal website http://www.ryanbrill.com/
Business site: http://www.infinitywebdesign.com

If you’re not using valid HTML, then you haven’t created a Web page. You may have created something else, but it isn’t a Web page. - Joe Clark
pyro is offline   Reply With Quote
Old 11-15-2003, 12:49 PM   #3
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
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 )
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 11-15-2003, 01:37 PM   #4
LordShryku
kung foo code monkey
 
LordShryku's Avatar
 
Join Date: Aug 2002
Location: Occupational Hypnotherapy
Posts: 7,473
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?
LordShryku is offline   Reply With Quote
Old 11-18-2003, 02:14 AM   #5
BuzzLY
2($infinity) && $beyond
 
BuzzLY's Avatar
 
Join Date: Nov 2002
Location: Star Command
Posts: 2,535
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...
__________________
New to the board? Check out the guidelines
| Color Picker | Blogification |
¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
With all its sham, drudgery, and broken dreams, it's still a beautiful world.
BuzzLY is offline   Reply With Quote
Old 11-19-2003, 09:43 PM   #6
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
Quote:
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. 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 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?
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 11-19-2003, 10:49 PM   #7
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,127
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.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 11-19-2003, 11:08 PM   #8
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Quote:
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.........
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:03 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.