Jump to content


Photo

str_replace problem


  • Please log in to reply
10 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 04 September 2006 - 12:04 PM

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

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

When the code is run I get the following error:

Parse error: parse error, unexpected ';' in LOCATION\PAGE.php(60) : eval()'d code on line 1


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?

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 12:07 PM

<?php

$calc="\$rate=".str_replace(array("L","E","F","I"), array('$l', '$e', '$f', '$I'),$field1).";";
eval($calc);

?>


#3 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 04 September 2006 - 12:10 PM

I added the ' ' around the 4 variables but still got the same error message

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 12:11 PM

change the eval to echo, what do you get?

#5 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 04 September 2006 - 12:15 PM

$rate=;



#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 12:16 PM

in that case: $field1 == ('' || null);

you need to look at where $field1 is declared and defined.

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 12:16 PM

Use
$calc = "\$rate = str_replace(array(\"L\",\"E\",\"F\",\"I\"), array(\$l, \$e, \$f, \$I), \$field1);";

If you are using eval.

However i see no use for eval here. Just use:
$rate = str_replace(array("L","E","F","I"), array($l, $e, $f, $I), $field1);


#8 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 04 September 2006 - 12:17 PM

Sorry - just realised I mispelt the field - hence no value!

Thanks for your time guys!

#9 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 04 September 2006 - 12:18 PM

Its always the simple things... lol

#10 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 01:11 PM

Use

$calc = "\$rate = str_replace(array(\"L\",\"E\",\"F\",\"I\"), array(\$l, \$e, \$f, \$I), \$field1);";

If you are using eval.

However i see no use for eval here. Just use:
$rate = str_replace(array("L","E","F","I"), array($l, $e, $f, $I), $field1);

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

#11 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 01:21 PM

Oh i see. Didnt realise that. Thanx for the heqads up.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users