easier way, part deux

This is a discussion on "easier way, part deux" within the PHP Forum section. This forum, and the thread "easier way, part deux 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 Jun 9th, 2007, 20:23
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
easier way, part deux

After an exhausting week of modularizing a ton of spaghetti code (and watching my page rank plummet to 0 as the entire site started throwing errors), I've finally gotten around to implementing some spacesaving. To-wit (as lawyers say):

There has to be an easier way

Now, I got the code Graham wrote to work just fine (had to substitute str_ireplace for preg_replace but iirc that's faster anyway), but I couldn't get this to work (stripped to bare minimum):
PHP: Select all

function formatFoodname() {
    global 
$food_name;
    global 
$str;
    include_once (
'./t_abbr.php');
    
$str=explode(','$str);
    foreach(
$str as $v) {
        
$abbr=strtok($v'.');
        
$word=strtok('.');
        
$food_name str_ireplace($abbr$word$food_name);
    }
}
$food_name='COMPOTE, PNUT, w/STWD, BOILED SEL';
formatFoodname();
echo 
$food_name
The 't_abbr.php' file is just a list of terms in the format -- well, example
PHP: Select all

<?php
$str
allpurp.all-purpose,
 
appl apple ,
asprt.aspartame,
bf.beef,;
?>
The tokenization and include appear to be working fine -- echo gets me a nice list of abbreviations and full words. I just can't get the string replacement to work using $abbr and $word as the needle and replacement. I also tried putting them into parallel arrays and still couldn't get the replacement to work, even though I had valid variables.

I tried dozens of variations with single/double quotes, etc. No go.

Anyone know what's wrong?
Reply With Quote

  #2 (permalink)  
Old Jun 13th, 2007, 14:00
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easier way, part deux

hmmm.... worked for me, but with str_replace instead of str_ireplace, but that makes it case sensitive

I got it to work with this code:
PHP: Select all

<?php

$str
'COMPOTE. I replace compote!!!,
 appl . apple ,
asprt.aspartame,
bf.beef'
;

function 
formatFoodname() {
    global 
$food_name;
    global 
$str;
    
$str=explode(','$str);
    foreach(
$str as $v) {
        
$abbr=strtok($v'.');
        
$word=strtok('.');
        
$food_name str_replace($abbr$word$food_name);
    }
}
$food_name='COMPOTE, PNUT, w/STWD, BOILED SEL';
formatFoodname();
echo 
$food_name;

?>
do you have php 5? str_ireplace is php 5 only

other than that... ???

Hope that helps!
Reply With Quote
  #3 (permalink)  
Old Jun 13th, 2007, 14:22
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easier way, part deux

Quote:
Originally Posted by jimz View Post
do you have php 5?
yar.
I'll give it another try, thx for the help. It's really more of an academic problem at this point, since I have functional code.

I need case insensitivity to handle case mismatching, since significant chunks of the database are copied from public sources with different case/abbreviation schemes. Feel my pain: we're talking @8000 rows (and growing) ranging in format from "PNT,BLD,W/O NA" to "Boiled peanuts, salt free".

(Actually I am extremely thankful to have them, but there are challenges. I do have a PHP system to expedite hand-entering rows and/or updating fields, but with 80 fields -- also growing -- hand-entering data is an horrendous chore.)

Last edited by masonbarge; Jun 13th, 2007 at 14:34.
Reply With Quote
  #4 (permalink)  
Old Jun 13th, 2007, 14:48
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easier way, part deux

Quote:
Originally Posted by masonbarge View Post
Feel my pain: we're talking @8000 rows (and growing) ranging in format from "PNT,BLD,W/O NA" to "Boiled peanuts, salt free".
Just reading that hurts...
Reply With Quote
Reply

Tags
php, replace, string

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
There has to be an easier way Donny Bahama PHP Forum 8 Apr 20th, 2007 12:07
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
SEO / SEM glossary Part 3 webulletin Search Engine Optimization (SEO) 2 Nov 22nd, 2006 23:33


All times are GMT. The time now is 20:57.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43