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 https://forums.phpfreaks.com/topic/248390-modify-a-string/ Share on other sites More sharing options...
AyKay47 Posted October 4, 2011 Share Posted October 4, 2011 not sure how you have this set up.. but what you need here are variables for the amount of each ingredient needed and a variable for the number of people.. $salt = 1; $sugar = 20; $something_nice = 15; $number_of_people = 3; if($number_of_people > 1){ $new_sugar = $sugar * $number_of_people; $new_something_nice = $something_nice * $number_of_people; $new_salt = $salt * $number_of_people; } i would create a function to do this instead if it will be needed multiple times.. also i assume that the data will be dynamically created.. so the values will be variables most likely. Quote Link to comment https://forums.phpfreaks.com/topic/248390-modify-a-string/#findComment-1275556 Share on other sites More sharing options...
jdunneirl Posted October 4, 2011 Author Share Posted October 4, 2011 Hi AyKay47, Would love to say it was that easy the problem is its coming from a wordpress database, so no idea how to get the ingredients in that manor, all I have is the string Quote Link to comment https://forums.phpfreaks.com/topic/248390-modify-a-string/#findComment-1275566 Share on other sites More sharing options...
Nodral Posted October 4, 2011 Share Posted October 4, 2011 Provided the values are all give as ##kg substance You can build a function to explode and return just the bit you need. eg function get_value($input){ $value=explode("kg",$input); $result=$value[0]; return $result; } Then if you do $salt=get_value(1kg of salt); .. .. .. repeat for all ingredients then use the script that AyKay wrote, you should be ok. If the string is coming out of wordpress in a variable eg $salt="1kg of salt", then just do $salt=get_value($salt) to split the number off. NOTE: This will only work if the string is in the format ##kg of substance Quote Link to comment https://forums.phpfreaks.com/topic/248390-modify-a-string/#findComment-1275651 Share on other sites More sharing options...
AbraCadaver Posted October 4, 2011 Share Posted October 4, 2011 Something like this should work. You can use it in a function and add more functionality if you want: preg_match('/([\d\.]+)([^\s]+)(.*)/', '20kg of sugar', $matches); $quantity = $matches[1]; $unit = $matches[2]; $ingredient = trim($matches[3]); You will also need to give some thought as to how to handle scaling up the units, for example when do you move from ml to l or g to kg, etc. Quote Link to comment https://forums.phpfreaks.com/topic/248390-modify-a-string/#findComment-1275656 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 https://forums.phpfreaks.com/topic/248390-modify-a-string/#findComment-1276284 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.