There has to be an easier way

This is a discussion on "There has to be an easier way" within the PHP Forum section. This forum, and the thread "There has to be an easier way are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Apr 18th, 2007, 04:50
Reputable Member
Join Date: Mar 2005
Location: Margaritaville (a state of mind somewhere between Inebriation and San Diego), CA
Posts: 233
Thanks: 4
Thanked 0 Times in 0 Posts
There has to be an easier way

Is there a better way of doing this? Something short and much more efficient? Seems like every time I do something like this, there's always a way to do it in just a line or two of code...
Code: Select all
switch ($MM) {
        case "01":
            $MM = "January ";
            break;
        case "02":
            $MM = "February ";
            break;
        case "03":
            $MM = "March ";
            break;
        case "04":
            $MM = "April ";
            break;
        case "05":
            $MM = "May ";
            break;
        case "06":
            $MM = "June ";
            break;
        case "07":
            $MM = "July ";
            break;
        case "08":
            $MM = "August ";
            break;
        case "09":
            $MM = "September ";
            break;
        case "10":
            $MM = "October ";
            break;
        case "11":
            $MM = "November ";
            break;
        case "12":
            $MM = "December ";
}
Reply With Quote

  #2 (permalink)  
Old Apr 18th, 2007, 12:28
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

That's pretty close to as well as you can do, afaik Donnie. You could create an array and use it, maybe with a foreach clause, and you'd have less code to load, but I don't think that there are any particular time savings involved either creating or running it. I sure wouldn't worry about it in that tiny bit. Here, let me prove that "mine's bigger than yours":

PHP: Select all

$food_name str_ireplace ('allpurp''all-purpose'$food_name);
$food_name str_ireplace (' and ''&'$food_name);
$food_name str_ireplace (' appl '' apple '$food_name);
$food_name str_ireplace ('asprt''aspartame'$food_name);
$food_name str_ireplace ('bf''beef'$food_name);
$food_name str_ireplace ('bn''bean'$food_name);
$food_name str_ireplace ('bkd''baked'$food_name);
$food_name str_ireplace ('bld''boiled'$food_name);
$food_name str_ireplace ('brsd''braised'$food_name);
$food_name str_ireplace ('brkfst''breakfast'$food_name);
$food_name str_ireplace ('btld''bottled'$food_name);
$food_name str_ireplace ('bttrmlk''buttermilk'$food_name);
$food_name str_ireplace (' cal '' calorie '$food_name);
$food_name str_ireplace (' choc '' chocolate '$food_name);
$food_name str_ireplace ('chpd''chopped'$food_name);
$food_name str_ireplace ('chopd''chopped'$food_name);
$food_name str_ireplace ('chs''cheese'$food_name);
$food_name str_ireplace (' choic '' choice '$food_name);
$food_name str_ireplace (' cinn '' cinnamon '$food_name);
$food_name str_ireplace ('ckd''cooked'$food_name);
$food_name str_ireplace ('cmdty''commodity'$food_name);
$food_name str_ireplace ('cnd''canned'$food_name);
$food_name str_ireplace ('commly''commercially'$food_name);
$food_name str_ireplace ('conc ''concentrated '$food_name);
$food_name str_ireplace ('crl''cereal'$food_name);
$food_name str_ireplace ('crwdr''crowder'$food_name);
$food_name str_ireplace ('crm''cream'$food_name);
$food_name str_ireplace ('cttnseed''cottonseed'$food_name);
$food_name str_ireplace ('cttnsd''cottonseed'$food_name);
$food_name str_ireplace ('dehyd ''dehydrated '$food_name);
$food_name str_ireplace ('drk''dark'$food_name);
$food_name str_ireplace (' di '' diced '$food_name);
$food_name str_ireplace ('drnd''drained'$food_name);
$food_name str_ireplace ('drsng''dressing'$food_name);
$food_name str_ireplace ('dssrt''dessert'$food_name);
$food_name str_ireplace ('enr''enriched'$food_name);
$food_name str_ireplace (' ex '' extra'$food_name);
$food_name str_ireplace ('fd''food'$food_name);
$food_name str_ireplace ('flav ''flavor '$food_name);
$food_name str_ireplace (' fort'' fortified'$food_name);
$food_name str_ireplace ('flr''flour'$food_name);
$food_name str_ireplace ('frsh''fresh'$food_name);
$food_name str_ireplace ('frstng''frosting'$food_name);
$food_name str_ireplace ('frz''frozen'$food_name);
$food_name str_ireplace ('grds''grades'$food_name);
$food_name str_ireplace ('grn''green'$food_name);
$food_name str_ireplace ('grns''greens'$food_name);
$food_name str_ireplace (' hi '' high '$food_name);
$food_name str_ireplace ('h2o''water'$food_name);
$food_name str_ireplace ('hvy''heavy'$food_name);
$food_name str_ireplace (' hydr '' hydrogenated '$food_name);
$food_name str_ireplace (' hydrog '' hydrogenated '$food_name);
$food_name str_ireplace ('imitn ''imitation '$food_name);
$food_name str_ireplace ('incl ''including '$food_name);
$food_name str_ireplace ('indus ''industrial '$food_name);
$food_name str_ireplace (' inf''infant'$food_name);
$food_name str_ireplace ('inst''instant'$food_name);
$food_name str_ireplace ('juc''juice'$food_name);
$food_name str_ireplace ('krnl''kernal'$food_name);
$food_name str_ireplace (' lt'' light'$food_name);
$food_name str_ireplace (' lo '' low '$food_name);
$food_name str_ireplace ('liq ''liquid '$food_name);
$food_name str_ireplace ('lmn''lemon'$food_name);
$food_name str_ireplace (' ln'' lean'$food_name);
$food_name str_ireplace ('lofat''low fat'$food_name);
$food_name str_ireplace (' ml'' meal'$food_name);
$food_name str_ireplace ('mxd''mixed'$food_name);
$food_name str_ireplace (' mture'' mature'$food_name);
$food_name str_ireplace ('mayo ''mayonnaise '$food_name);
$food_name str_ireplace ('med ''medium '$food_name);
$food_name str_ireplace (' mshd '' mashed '$food_name);
$food_name str_ireplace (' msh '' mashed '$food_name);
$food_name str_ireplace (' nat'' natural'$food_name);
$food_name str_ireplace (' na '' salt '$food_name);
$food_name str_ireplace (' pot '' potato '$food_name);
$food_name str_ireplace ('prtrhs''porterhose'$food_name);
$food_name str_ireplace ('prot ''protein '$food_name);
$food_name str_ireplace ('prep''prepared'$food_name);
$food_name str_ireplace ('pnt''peanut'$food_name);
$food_name str_ireplace ('pln''plain'$food_name);
$food_name str_ireplace ('pk ''pack '$food_name);
$food_name str_ireplace ('pnut''peanut'$food_name);
$food_name str_ireplace ('pudd''pudding'$food_name);
$food_name str_ireplace ('pdr''powder'$food_name);
$food_name str_ireplace ('pwdr''powder'$food_name);
$food_name str_ireplace (' rtl'' retail'$food_name);
$food_name str_ireplace (' rstd'' roasted'$food_name);
$food_name str_ireplace (' rst'' roast'$food_name);
$food_name str_ireplace ('rnd''round'$food_name);
$food_name str_ireplace ('red fat''reduced fat'$food_name);
$food_name str_ireplace (' reg ''regular'$food_name);
$food_name str_ireplace ('refr ''refrigerated '$food_name);
$food_name str_ireplace (' sau '' sauce '$food_name);
$food_name str_ireplace (' sd'' seed'$food_name);
$food_name str_ireplace (' sel '' select '$food_name);
$food_name str_ireplace ('sgr''sugar'$food_name);
$food_name str_ireplace ('skn''skin'$food_name);
$food_name str_ireplace ('smmr ''summer '$food_name);
$food_name str_ireplace (' sol '' solid '$food_name);
$food_name str_ireplace ('soybn''soybean'$food_name);
$food_name str_ireplace ('spce''spice'$food_name);
$food_name str_ireplace ('spl''special'$food_name);
$food_name str_ireplace ('sprd''spread'$food_name);
$food_name str_ireplace ('stk''stick'$food_name);
$food_name str_ireplace ('stmd''steamed'$food_name);
$food_name str_ireplace (' str '' strained '$food_name);
$food_name str_ireplace ('sthrn''southern'$food_name);
$food_name str_ireplace ('stwd''stewed'$food_name);
$food_name str_ireplace ('swt''sweet'$food_name);
$food_name str_ireplace ('swtnd''sweetened'$food_name);
$food_name str_ireplace ('sndwch''sandwich'$food_name);
$food_name str_ireplace ('sub ''substitute '$food_name);
$food_name str_ireplace ('whl''whole'$food_name);
$food_name str_ireplace 'yel '' yellow '$food_name);
$food_name str_ireplace (' var '' varieties '$food_name);
$food_name str_ireplace (' veg '' vegetable '$food_name); 
$food_name str_ireplace (' tom '' tomato '$food_name); 
$food_name str_ireplace (' unspec '' unspecified '$food_name); 
$food_name str_ireplace ('&'' & '$food_name);
 
$food_name str_ireplace ('armour''<span class="i">Armour</span>'$food_name);
$food_name str_ireplace ('banquet''<span class="i">Banquet</span>'$food_name);
$food_name str_ireplace ('betty crocker''<span class="i">Betty Crocker</span>'$food_name);
$food_name str_ireplace ('breyers''<span class="i">Breyers</span>'$food_name);
$food_name str_ireplace ('castleberry''<span class="i">Castleberry</span>'$food_name);
$food_name str_ireplace ('chef boyardee''<span class="i">Chef Boyardee</span>'$food_name);
$food_name str_ireplace ('el rio''<span class="i">El Rio</span>'$food_name);
$food_name str_ireplace ('custom foods''<span class="i">Custom Foods</span>'$food_name);
$food_name str_ireplace ('general mills''<span class="i">General Mills</span>'$food_name);
$food_name str_ireplace ('heinz''<span class="i">Heinz</span>'$food_name);
$food_name str_ireplace ('hnz''<span class="i">Heinz</span>'$food_name);
$food_name str_ireplace ('kellog\'s''<span class="i">Kellogg\'s</span>'$food_name);
$food_name str_ireplace ('knorr''<span class="i">Knorr</span>'$food_name);
$food_name str_ireplace ('kraft''<span class="i b">Kraft</span>'$food_name);
$food_name str_ireplace ('Lipton Cup-a-soup''<span class="i">Lipton Cup-a-Soup</span>'$food_name);
$food_name str_ireplace ('Lipton soup secrets''<span class="i">Lipton Soup Secrets</span>'$food_name);
$food_name str_ireplace ('nestle''<span class="i">Nestle</span>'$food_name);
$food_name str_ireplace ('pepperidge farm''<span class="i">Pepperidge Farm</span>'$food_name);
$food_name str_ireplace (' rte'' RTE'$food_name);
$food_name str_ireplace ('wo/''without '$food_name);
$food_name str_ireplace ('w/''with '$food_name);
$food_name str_ireplace (' or '' or '$food_name); 
Reply With Quote
  #3 (permalink)  
Old Apr 18th, 2007, 15:17
Reputable Member
Join Date: Mar 2005
Location: Margaritaville (a state of mind somewhere between Inebriation and San Diego), CA
Posts: 233
Thanks: 4
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

Thanks for the reply, Mason. Wow- can't believe I got it right the first time! I must be getting the hang of this php stuff!
Reply With Quote
  #4 (permalink)  
Old Apr 19th, 2007, 03:25
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

Quote:
Originally Posted by Donny Bahama View Post
Is there a better way of doing this? Something short and much more efficient? Seems like every time I do something like this, there's always a way to do it in just a line or two of code...
Yes, the general rule in PHP is that if you find yourself repeating something common, there has to be an easier way. How about

Code: Select all
<?php
$inval = "09";

$tsample = mktime(12,0,0,$inval,15,2007);
$mname = date("F",$tsample);

print ("It is the month of $mname");
?>
which uses the already-provided tables of month names within the date function ...
Reply With Quote
  #5 (permalink)  
Old Apr 19th, 2007, 04:01
Reputable Member
Join Date: Mar 2005
Location: Margaritaville (a state of mind somewhere between Inebriation and San Diego), CA
Posts: 233
Thanks: 4
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

Thanks for the response, grahame! Great rule of thumb; I'll keep that in mind in the future.

Given that $MM is a preexisting inval in my code (as well as the desired outval), I can then adapt your code to mine like so...
Code: Select all
$MM = date("F",mktime(1,1,1,$MM,1,98));
Does that look about right?
Reply With Quote
  #6 (permalink)  
Old Apr 19th, 2007, 12:45
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

Looks good ...
Reply With Quote
  #7 (permalink)  
Old Apr 19th, 2007, 13:37
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

Nice code, Graham -- I'll have some fun trying to trim my own bloat (bigger than Donny's, wot?) when I have the time, but my to-do list is rather long with things that don't work right, LOL.
Reply With Quote
  #8 (permalink)  
Old Apr 19th, 2007, 17:16
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

Masonbarge, I would tend to move all of your specific text OUT into a separate and easy to maintain data file rather than leave it in the program - the data might look like:

Code: Select all
allpurp,all-purpose,
 and , &amp; ,
 appl , apple ,
asprt,aspatame,
bf,beef,
bn,bean,
etc
and then the conversion could be shortened to:

Code: Select all
<?php
$food_name = " appl and bf pie";
print ("$food_name<br>\n");
foreach (file("changeit.txt") as $line) {
        list($change,$into) = explode(",",$line);
        $food_name = preg_replace("/$change/i",$into,$food_name);
        }
print ("$food_name<br>\n");
?>
Of course, if you get paid by the line of code written or want to retain control in the programmer's hands, this approach probably isn't for you
Reply With Quote
  #9 (permalink)  
Old Apr 20th, 2007, 12:07
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: There has to be an easier way

Thanks so much for the pointer. I'll play around with it this morning. My instinct is to use str_replace rather than preg_replace, do you see any problem with that?

Or since I only use it on a query resource -- 80%+ of my database is from the USDA (US Dept of Agriculture), which is in all caps and uses these horrid overlapping abbreviations -- maybe I'll try just putting up a little table in the same db and figure out a way to combine the two searches. That'd keep the buggers out of my code and give me a nice array right off.

Edit: Hmm, now you really have me stirred up. If I have the guts I may just run a search/replace on the database and solve the problem once and for all - replace the abbreviations and put it in lower case.
Quote:
Of course, if you get paid by the line of code written . . . this approach probably isn't for you
:dry: Well, Dickens got paid by the word (and schoolchildren have paid dearly for it ever since).

Last edited by masonbarge; Apr 20th, 2007 at 12:15.
Reply With Quote
Reply

Tags
arrays, date parsing, months

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Making MHT-CET easier! RohanShenoy Free Web Site Critique 32 Dec 26th, 2007 22:54
easier way, part deux masonbarge PHP Forum 3 Jun 13th, 2007 14:48
Css/xhtml makes life so much easier pa007 Webforumz Cafe 23 Apr 17th, 2007 11:36
An easier way to edit CSS fonts stevesnz Web Page Design 3 Apr 9th, 2007 11:12


All times are GMT. The time now is 11:36.