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?