Jump to content

Modify a string


jdunneirl

Recommended Posts

Hi guys,

 

this is my first post so be gentle :D

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.