jdunneirl Posted October 4, 2011 Share Posted October 4, 2011 Hi guys, this is my first post so be gentle I have a string of ingredients, eg 1kg of salf 20kg of sugar 15kg of something nice This is the portion / mix for 1 person, I would like to make it scalable, so for example multiple all ingredients by for two people etc. So how do I do this with PHP I am guessing a regexp to look at the string and modify each number by the multiplier Any suggestions would be great thanks guys I have tried google and came up empty I think I need to convert the string to an array then modify it? John Quote Link to comment Share on other sites More sharing options...
.josh Posted October 4, 2011 Share Posted October 4, 2011 You have 2 major issues to overcome here: 1) Figuring out which numbers should be multiplied and which numbers should not. At face value, you can do something like... $content = preg_replace("~\d+~e",'$n*$0',$content); ..where $n is the multiplier. But that isn't really context specific. So for instance if you happen to have other numbers within your content that aren't ingredient measurements, they will be affected (eg: "...cook at 300 degrees" would turn into "...cook at 600 degrees"). 2) Working out measurements given as fractions or decimals. For example, if you have "1/2tsp salt" you will end up with "2/4tsp salt", or "1.5tbsp butter" would make for "2.10tbsp butter". How you solve this mostly depends on how you have your content coded to begin with. Do you have control over how your content is coded? For example, can you wrap some kind of tag around all instances of numbers that should be multiplied? If so, then figuring out issue #1 would be easy. But if not...I'm afraid there's probably no accurate way to do that. You might possibly be able to do something with the ingredient list because it might be wrapped in a separate html div or have list tags wrapped around it or something, but probably not within the instructions. Show some examples of real content (the actual html code). As for working out #2...well that's largely dependent on #1, but in general, even if you could isolate which numbers should be multiplied, decimal to fraction conversions (or visa versa) isn't exact (for instance, 1/3 makes for 1.3333....infinity more 3's. You can round off easy enough but..go ahead and try finding an easy, accurate way to convert that back to 1/3!) Quote Link to comment Share on other sites More sharing options...
xyph Posted October 5, 2011 Share Posted October 5, 2011 I'd suggest trying it like this: <?php $recipe = '%d kg of salf %d kg of sugar %d kg of something nice'; $people = 2; $salt = 1 * $people; $sugar = 20 * $people; $nice = 15 * $people; echo sprintf($recipe, $salt,$sugar,$nice); ?> Removing the values, changing them, and putting them back in is a SERIOUS hassle and can be inaccurate as .josh stated. Quote Link to comment Share on other sites More sharing options...
jdunneirl Posted October 5, 2011 Author Share Posted October 5, 2011 Thanks for all the help guys I managed to strip the function that was given me the values so now I got the numbers I need thanks again John 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.