ultrus Posted February 22, 2007 Share Posted February 22, 2007 Howdy, At work, one of the annoying tasks I hate doing is adding a % markup to printing quotes as they come in. I sift through long emails from the printer, manually adding the markup to each price using a calculator before sending it to the client. Why not just sift the email through a php function, and pop out the same email, with new prices? I'm getting close, but am unsure on the best way to do this. Below is some partial script for this function. How do I fill in the gaps? Also, how do I get the dollar amount without the '$'? <?php function addMarkups($text) { $pattern = "\$[0-9.]+"; //how do I get the dollar amount after the $, but without the $? //??? - formula used is dollar amount (without the dollar sign) divided by .9 $replacement = ""; //pehaps I don't use this variable, and jsut use the formula in the replacement placeholder below? $text = preg_replace($pattern, $replacement, $text); return $text; } $exampleMarkup = addMarkups("It's going to cost $20.59 to build a house, $350 to clean it, and $4.40 to get groceries."); echo $exampleMarkup; ?> Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted February 22, 2007 Share Posted February 22, 2007 Get the email file (in a txt file or something) run it through number_format and string replace. Or take all numbers from the email, change them into the format you want (all regular expressions), then replace the old numbers with the new ones. Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 22, 2007 Author Share Posted February 22, 2007 Getting the contents into php is not a problem. I don't want to replace all numbers in the email as the printer might say something like: 1/1 1PMS 5000 #10 envilopes: $400 6000: $420.50 It will be $500 for 8000 number10 That would cause more work for me if I had to replace all the other numbers that were "marked up". This script is all about being lazy. hehe. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 22, 2007 Share Posted February 22, 2007 Are you able to isolate the data you want to modify from the data you don't? If not, how can you tell them apart (by the preceding ": ")? Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 22, 2007 Share Posted February 22, 2007 Alright I played around for alittle bit and I think you're looking for something like this. It is acting kind of funny though, because it makes an array inside of an array..not sure why.. <?php function addMarkups($text) { $pattern = '/[0-9]+[,][0-9]{3}[,][0-9]{3}[.][0-9]+|[0-9]+[,][0-9]{3}[,][0-9]{3}|[0-9]+[,][0-9]+[.][0-9]+|[0-9]+[,][0-9]{3}|[0-9]+[.][0-9]+|[0-9]+/'; preg_match_all($pattern, $text,$matches); return $matches; } $test = addMarkups("It's going to cost $20.59 to build a house, $350 to clean it, and $4.40 to get groceries. Bah, u owe me $5,000.21. And 5,001 and 5,000,000 and 6,000,000.11."); $test = $test[0]; foreach($test as $t) { print_r($t."<br>"); } ?> Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 22, 2007 Author Share Posted February 22, 2007 Hello magic2goodil, Thanks for your efforts! I will play witho your script in a minute. I've been progressing as well with a script that is acting funny: <?php function addMarkups($text) { $pattern = '/[\$]+[0-9.]+/'; //need to modify this to accomidate "It will be $12.50." - note the period $numMatches = preg_match_all($pattern, $text, $dollarAmounts); for($i=0; $i<$numMatches; $i++) { //replacement for every pattern $patterns[$i] = "/\\" . $dollarAmounts[0][$i] . "/"; //remove "$", add markup, then turn it back into a string $newDollarAmounts[$i] = "\$" . number_format(substr($dollarAmounts[0][$i], 1, strlen($dollarAmounts[0][$i]) - 1) / .8, 2, '.', ''); //looks like everything is good so far echo("old ammount: " . $dollarAmounts[0][$i] . "<br>"); echo("new ammount: " . $newDollarAmounts[$i] . "<br>"); } //replace down the list //what's going wrong here?? $text = preg_replace($patterns, $newDollarAmounts, $text); return $text; } $exampleMarkup = addMarkups("It's going to cost \$20.59 to build a house, \$350 to clean it, and \$4.40"); echo $exampleMarkup; ?> It returns this: old ammount: $20.59 new ammount: $25.74 old ammount: $350 new ammount: $437.50 old ammount: $4.40 new ammount: $5.50 It's going to cost .74 to build a house, 7.50 to clean it, and .50 Any ideas on what I'm doing wrong? Ah! Good pointers on the 1,000 comma stuff. Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 22, 2007 Share Posted February 22, 2007 Keep on mind when doing your patterns, put the more advanced and bigger ones first. Such as a patten to retrieve 1,000,000.00, then one to retrieve, 1,000,000 without the .00, then 5,000.00, then 5,000, etc.. That way it checks for the more complex ones first and u don't get duplicates or messed up data. That could be why you're only retrieving the values after the decimal in the end. Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 22, 2007 Author Share Posted February 22, 2007 Cool. I will keep that in mind. Today is the first day I have actually made good use of patterns. This is good practice. Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 22, 2007 Share Posted February 22, 2007 I'm still pretty newbish to patterns. One day I read bunch on them and was an expert for a day, and today I looked for 15 minutes and played around for another 10 or so before I sort of remembered how to do stuff with them, lol Quote Link to comment Share on other sites More sharing options...
effigy Posted February 22, 2007 Share Posted February 22, 2007 Not including numbers in the thousands... <pre> <?php function markup ($matches) { $old_num = $matches[0]; $new_num = number_format($old_num / .8, 2, '.', ''); echo "$old_num => $new_num<br>"; return $new_num; } function addMarkups($text) { return preg_replace_callback('/(?<=\$)[0-9.]+\b/', 'markup', $text); } echo addMarkups("It's going to cost \$20.59 to build a house, \$350 to clean it, and \$4.40 to get groceries. It will be $12.50."); ?> </pre> Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted February 22, 2007 Share Posted February 22, 2007 On a side note: I must visit your company if they can build me a house for less than $30 and get me groceries for under $5 But I will however not have them clean it since it costs over $400 to do such. Quote Link to comment Share on other sites More sharing options...
ultrus Posted February 22, 2007 Author Share Posted February 22, 2007 loL! Yeah I must have been on crack when I wrote that. effigy, That works great! I added the thousands touches below. Thanks much for the help everyone <?php function markup ($matches) { $old_num = preg_replace("/[,]+/","",$matches[0]); $new_num = number_format($old_num / .8, 2, '.', ','); echo "$old_num => $new_num<br>"; return $new_num; } function addMarkups($text) { return preg_replace_callback('/(?<=\$)[0-9.,]+\b/', 'markup', $text); } echo addMarkups("It's going to cost \$20.59 to build a house, \$350 to clean it, and \$4.40 to get groceries. It will be $12.50. A whopping $1,000. $5,000,000.00 is big. Don't eat my $2,123,944 burger."); ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted February 23, 2007 Share Posted February 23, 2007 str_replace would be better for handling the thousands, as regular expressions are overkill for removing commas. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.