Jump to content

Find all numbers in a string and multiply


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

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!)

 

 

 

Link to comment
Share on other sites

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.

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.