Jump to content

[SOLVED] requesting help with simple replace+math script


ultrus

Recommended Posts

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;

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>");

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.");

?>

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.