> Sven Dzepina wrote:

> >Hello people =)

> >

> >Has somebody a nice script, which can solve equations ?

>

> yes, I do now :-)

>

> >It would be super, if someone has an idea where I can get such a script /

> >code in php.

>

> Use mine

>

>

> you're welcome :-)))

>

>

>

>

> <?php // solve equation

> function solve($eq) {

> $eq = str_replace(' ', '', $eq);

>

> // validate expression

> $terms = explode('=', $eq);

> if (count($terms) != 2) {

> return('syntax error: you must have one (and only one) "=" in ['

> . $eq . '].');

> }

>

> // put rhs in lhs

> $eq = $terms[0] . '-(' . $terms[1] . ')';

>

> if (substr_count($eq, '(') != substr_count($eq, ')')) {

> return('syntax error: parenthesis mismatch in [' . $eq . '].');

> }

> if (preg_match('/\([^)]*\(/', $eq)) {

> return('syntax error: only one level of parenthesis allowed in ['

> . $eq . '].');

> }

> if (preg_match('/[^x0-9+*\/().=-]/', $eq)) {

> return('syntax error: the only valid characters are x, 0-9, +, '

> . '*, /, (, ), ., and - in [' . $eq . ']');

> }

> if (preg_match('/[^x0-9\)][-+\/*]/', $eq)) {

> return('syntax error: you can only operate on numbers in ['

> . $eq . ']');

> }

> if (preg_match('/[-+\/*][^x0-9\(]/', $eq)) {

> return('syntax error: you can only operate on numbers in ['

> . $eq . ']');

> }

>

> $eq = str_replace('x', '$x', $eq);

> $y0 = $y1 = 0;

> $x = 0; eval('$y0 = ' . $eq . ';');

> $x = 1; eval('$y1 = ' . $eq . ';');

> $slope = $y1 - $y0;

> $intersect = $y0;

> return (- ($intersect / $slope));

> }

>

> $eq = '2 * (x + 4) = 10';

> echo $eq, ' ==> x = ', solve($eq), '<br />';

>

> $eq = '(2 * x) + 4 = 10';

> echo $eq, ' ==> x = ', solve($eq), '<br />';

>

> $eq = '2 * x + 4 = 10';

> echo $eq, ' ==> x = ', solve($eq), '<br />';

>

> $eq = '4 + x / 10 = 2';

> echo $eq, ' ==> x = ', solve($eq), '<br />';

> ?>

>

>

> --

> I have a spam filter working.

> To mail me include "urkxvq" (with or without the quotes)

> in the subject line, or your mail will be ruthlessly discarded.

## Bookmarks