Member Avatar for LastMitch

Hi

I'm having an issue echo out the euro currency sign with the price converted base on the $exchange_rate = 0.7746;

Here is my code:

<?php

$currency = true;

$format = '&euro; %2f';

$exchange_rate = 0.7746;

function currency_valve($data)

{

$price = $data[1];

$cpercent = $data[2];

$cvalve = isset ($_GLOBALS['currency']) && $_GLOBALS['currency'];

$amount = ($cvalve ) ? $price * (1 + $cpercent / 100) : $price;

return sprintf($GLOBALS['format'], $amount / $GLOBALS['exchange_rate']);

}

$data = "This Mango Cheesecake costs {amount: 11.95} "."and the Raspberry Cheesecake costs {amount: 13.95}.\n";

echo preg_replace_callback ('/\{amount\:\ ([0-9.]+)\ \%([0-9.]+)\%\}/','currency_valve',$data);
?>

When I echo it:

This Mango Cheesecake costs {amount: 11.95} and the Raspberry Cheesecake costs {amount: 13.95}.

It should convert the euro price / exchange rate = 0.7746 = new euro price

The correct statement should look like this:

This Mango Cheesecake costs {euro 11.95} and the Raspberry Cheesecake costs {euro 13.95}.

Any Suggestions and explanation will help. I appreciate it. Thanks!

Member Avatar for LastMitch

@pritaeas

Thanks for the reply and the catch. I will made an adjustment.

Member Avatar for LastMitch

OK this is my new code:

<?php

$currency = true;

$format = '&euro; %2f';

$exchange_rate = 0.7746;

function currency_valve($data)
{

$price = $data[1];

$cpercent = $data[2];

$cvalve = isset ($GLOBALS['currency']) && $GLOBALS['currency'];

$amount = ($cvalve ) ? $price * (1 + $cpercent / 100) : $price;

return sprintf($GLOBALS['format'], $amount / $GLOBALS['exchange_rate']);

}

$data = "This Mango Cheesecake costs {amount: 11.95} "."and the Raspberry Cheesecake costs {amount: 13.95}.\n";

echo preg_replace_callback ('/\{amount\:\ ([0-9.]+)\ \%([0-9.]+)\%\}/','currency_valve',$data);

?>

When I ran it, it came out: Undefined variable: _GLOBALS

So I took out the underscores for all GLOBALS so there was no error

Instead it still echo:

This Mango Cheesecake costs and the Raspberry Cheesecake costs .

commented: To Rectify what some retard did to LastMitch +0

Apart from the fact I absolutely hate the use of globals, you can try redefining global $format; in the function, or define the variables in the function as they are not used outside it, or use defines.

commented: Thanks for the explanation! +6
Member Avatar for diafol

I find having to use $GLOBALS very clumsy. Is there no other way to pass data to your function? As a parameter? If the variables are 'constant', define them as constants and they will be available to all code (functions, methods etc). Note that you can't change the value of a constant.

As for the function itself, it looks very complicated for what is essentially a conversion routine.

Here's one I made earlier. However, it uses the 'e' modifier - something that could be a security hole. You'd need to validate all vars before using it.

function prettifyProduct($input,$rate,$fmt){
    return $fmt . number_format($input * $rate, 2,'.',','); 
}

function convertMoney($data, $exchange_rate= '1', $format='$ '){
    return preg_replace("/(\d+\.\d+)/e","prettifyProduct('$1','$exchange_rate','$format')",$data);
}

$data = "This Mango Cheesecake costs 11.95 and the Raspberry Cheesecake costs 13.95.";
$format = '&euro; ';
$exchange_rate = 0.7746;

//ORIGINAL WITH $ symbol
echo convertMoney($data);
//EURO CONVERSION
echo convertMoney($data,$exchange_rate,$format);

I would imagine that a better use would be to have all the 'euro' details in an array, along with other currencies (formats, rates). Should be just as easy. Maybe :

function prettifyProduct($input,$currency){
    $c_array = array(
        "us"            =>  array('rate'=>1,'format'=>'$ '),
        "euro"          =>  array('rate'=>0.7714,'format'=>'&euro; '),
        "sterling"      =>  array('rate'=>0.6199,'format'=>'&pound; '),
        "yen"           =>  array('rate'=>8.0000,'format'=>'&yen; '));

    return $c_array[$currency]['format'] . number_format($input * $c_array[$currency]['rate'], 2,'.',',');  
}

function convertMoney($data, $currency = 'us'){
    return preg_replace("/(\d+\.\d+)/e","prettifyProduct('$1','$currency')",$data);
}

$data = "This Mango Cheesecake costs 11.95 and the Raspberry Cheesecake costs 13.95.";

echo convertMoney($data) . "<br />";
echo convertMoney($data,'euro') . "<br />";
echo convertMoney($data,'yen') . "<br />";
echo convertMoney($data,'sterling');
commented: Thanks for the example and explanation +6
Member Avatar for LastMitch

@pritaeas

you can try redefining global $format; in the function, or define the variables in the function as they are not used outside it, or use defines.

I will make some adjustment with global $format;

Member Avatar for LastMitch

@diafol

Is there no other way to pass data to your function? As a parameter? If the variables are 'constant', define them as constants and they will be available to all code (functions, methods etc). Note that you can't change the value of a constant.

Thanks for the reply and explanation. I will test out your example! Yes I know I can't change the value of constant.

Member Avatar for LastMitch

@pritaeas

The global $format; didn't help much because I got alot of errors: Undefined variable: format

So I went back using $globals

Member Avatar for LastMitch

@diafol

I got to test out your 2 scripts. It's really neat! Your conversion rate is all right! My conversion rate is wrong. I got no idea how to make my rates to look like yours.

Member Avatar for LastMitch

@pritaeas
@diafol

I figure out how to make it work correctly but my conversion rate is wrong. It's really different from diafol examples because his rates are right.

Here is my code:

<?php

$currency = true;

$format = '&euro; %.2f';

$exchange_rate = 0.7746;

function currency_valve($data)
{

$price = $data[1];

$cpercent = $data[2];

$cvalve = isset ($GLOBALS['currency']) && $GLOBALS['currency'];

$amount = ($cvalve ) ? $price * (1 + $cpercent / 100) : $price;

return sprintf($GLOBALS['format'], $amount / $GLOBALS['exchange_rate']);

}

$data = "This Mango Cheesecake costs {amount: 11.95 %0%} "."and the Raspberry Cheesecake costs {amount: 13.95 %0%}.\n";

echo preg_replace_callback ('/\{amount\:\ ([0-9.]+)\ \%([0-9.]+)\%\}/','currency_valve',$data);

?>

When I run the code and echo it out:

This Mango Cheesecake costs € 15.43 and the Raspberry Cheesecake costs € 18.01.

I realized I screw up the calculation.

When I run diafol examples it came out like this:

This is example 1:

This Mango Cheesecake costs $ 11.95 and the Raspberry Cheesecake costs $ 13.95.
This Mango Cheesecake costs € 9.26 and the Raspberry Cheesecake costs € 10.81.

This is example 2:

This Mango Cheesecake costs $ 11.95 and the Raspberry Cheesecake costs $ 13.95.
This Mango Cheesecake costs € 9.22 and the Raspberry Cheesecake costs € 10.76.
This Mango Cheesecake costs ¥ 95.60 and the Raspberry Cheesecake costs ¥ 111.60.
This Mango Cheesecake costs £ 7.41 and the Raspberry Cheesecake costs £ 8.65.

Any Suggestions and explanation why my conversion is screw up? I appreciate it. Thanks!

Try:

$amount = $cvalve ? $price * (1 + $cpercent) / 100 : $price;

This will multiply before dividing, so it could round differently.

Member Avatar for LastMitch

@pritaeas

I will test out that code. Thanks

Member Avatar for LastMitch

@pritaeas
@diafol

@pritaeas It didn't work.

I try every possible way to make the conversion price to look like diafol.

I'm just gonna to let it go. The example is working but the calculation is off.

I want to thank both of you for being patience and helping me out with this issue.

Thanks!

Member Avatar for diafol

Don't give up - you're obviously trying to get something done with the percentage. If you could explain - you have a percentage following a price. That percentage of the price is supposed to add onto the original amount to give a new price? So like a discount / levy?

I'd do this, but I'm sure somebody can tidy up my regex (I'm rubbish at it):

function prettifyProduct($input,$currency){
    $i = explode(" ",substr($input,1,-1));
    $c_array = array(
        "us"            =>  array('rate'=>1,'format'=>'$ '),
        "euro"          =>  array('rate'=>0.7714,'format'=>'&euro; '),
        "sterling"      =>  array('rate'=>0.6199,'format'=>'&pound; '),
        "yen"           =>  array('rate'=>8.0000,'format'=>'&yen; '));
    $price = $i[0] + $i[1]/100 * $i[0]; //REM bo-dmas - div,multi,add,substract
    return $c_array[$currency]['format'] . number_format($price * $c_array[$currency]['rate'], 2,'.',',');  
}
function convertMoney($data,$currency = 'us'){
    return preg_replace("/(\{\d+\.?\d* -?\d+\.?\d*\})/e","prettifyProduct('$1','$currency')",$data);
}

$data = "This Mango Cheesecake costs {11.95 -20.0} and the Raspberry Cheesecake costs {13.95 10.0}.";

BTW, this isn't meant as a solution, just to show the calculation works.

Member Avatar for LastMitch

@diafol

Don't give up - you're obviously trying to get something done with the percentage.

Yeah, You're right. I did give up not because of the script it's working it's the calucation that making me frustrated. The couple of nights.

If you could explain - you have a percentage following a price. That percentage of the price is supposed to add onto the original amount to give a new price? So like a discount / levy?

$data = "This Mango Cheesecake costs {11.95 -20.0} and the Raspberry Cheesecake costs {13.95 10.0}.";

I never thought about a discount. It didn't work. It only accept a percentage % with a number.

This %0% was a lucky guess because I had to look up sprintf() function to find out about it.

Here is the link for sprintf() function

http://php.net/manual/en/function.sprintf.php

Yes, I understand your calucation because it's more modern than mines.

This example was a on my exam that I took a long time ago. I got 70 on my test. But I got this wrong because I couldn't figure out how to convert the symbol plus it's hard to remember every function.

I rarely see anyone used sprintf() function or preg_replace_callback() function on same code.

I might have to do some research on strings.

Member Avatar for diafol

sprintf() has its uses - but as a rule I use it as a placeholder, similar to parameter binding in PDO. The preg_replace_callback() also has its uses, but for me, getting it to run a function that has many parameters (in order to avoid globals) fries my brain.

Member Avatar for LastMitch

@diafol

sprintf() has its uses - but as a rule I use it as a placeholder, similar to parameter binding in PDO. The preg_replace_callback() also has its uses, but for me, getting it to run a function that has many parameters (in order to avoid globals) fries my brain.

I might have to agree with you. I'm not familiar with PDO and I know pritaeas wrote a code snippet. I have to read more about it. Thanks for the encouragement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.