Jump to content

str_replace problem


master82

Recommended Posts

In my table i have the field 'field1' and an example of its data is below:
[code]
((F*0.8)/1)+(L/4)
[/code]

Its simply a small calculation, which uses the following code:
[code]
$calc="\$rate=".str_replace(array("L","E","F","I"), array($l, $e, $f, $I),$field1).";";
eval($calc);
[/code]

When the code is run I get the following error:
[quote]
Parse error: parse error, unexpected ';' in LOCATION\PAGE.php(60) : eval()'d code on line 1
[/quote]

Any idea what it is i'm doing wrong?

Also Im using $rate later on in the page - will there be a problem with this?
Link to comment
https://forums.phpfreaks.com/topic/19660-str_replace-problem/
Share on other sites

Use
[code=php:0]$calc = "\$rate = str_replace(array(\"L\",\"E\",\"F\",\"I\"), array(\$l, \$e, \$f, \$I), \$field1);";[/code]

If you are using eval.

However i see no use for eval here. Just use:
[code=php:0]$rate = str_replace(array("L","E","F","I"), array($l, $e, $f, $I), $field1);[/code]
Link to comment
https://forums.phpfreaks.com/topic/19660-str_replace-problem/#findComment-85680
Share on other sites

[quote author=wildteen88 link=topic=106833.msg427692#msg427692 date=1157372216]
Use
[code=php:0]$calc = "\$rate = str_replace(array(\"L\",\"E\",\"F\",\"I\"), array(\$l, \$e, \$f, \$I), \$field1);";[/code]

If you are using eval.

However i see no use for eval here. Just use:
[code=php:0]$rate = str_replace(array("L","E","F","I"), array($l, $e, $f, $I), $field1);[/code]
[/quote]it appears that $field1 contains an equation, the OP is trying to replace Keywords with values, and then execute the equation. eval() will be needed if the OP wants that equation to execute in PHP :)
Link to comment
https://forums.phpfreaks.com/topic/19660-str_replace-problem/#findComment-85702
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.