Jump to content

little help with pattern with maths


skullJ

Recommended Posts

As I posted here i need a pattern to replace all alphanumerics only

between spaces, math symbols ( +-*/^), parentheses ( )and allready math fuctions like ABS, ATN, SQR, e, pi .

 

examples:

c = (l1 ^ 2 - l2 ^ 2 + xb ^ 2 + yb ^ 2) / 2
ya1 = ((2 * c * yb) + SQR(D)) / (2 * (xb ^ 2 + yb ^ 2))

Any help?

Link to comment
Share on other sites

Hi, I need to replace all alphanumerics variables from quote below, with numbers, using preg_replace

 

c = (l1 ^ 2 - l2 ^ 2 + xb ^ 2 + yb ^ 2) / 2
ya1 = ((2 * c * yb) + SQR(D)) / (2 * (xb ^ 2 + yb ^ 2))

 

Here it is an example (using str_replace) of what i want to do:

 

<?php
include('RPN.php');

function do_maths($expr){
global $variables;
foreach($variables as $key => $value) {
$expr = str_replace($key, $value, $expr);
}
     $rpn = new Math_Rpn();
     $result = $rpn->calculate($expr,'deg',false);

return $result;
}

$variables['l1'] = 100;
$variables['l2'] = 50;
$variables['x'] = 60;
$variables['y'] = 30;
$phrase = "c = (l1 ^ 2 - l2 ^ 2 + x ^ 2 + y ^ 2) / 2";
$phrase = explode('=',$phrase);
$result =  do_maths($phrase[1]);

echo $result; // the result is 6000
?>

 

Thank you

Link to comment
Share on other sites

Well, I'm still lost. Can you provide an example of what you want the output of that quote to be? Do you have specific values to use as replacement for the individual patterns? Your request also assumes that you want to replace the numbers with numbers as well  :confused: .

 

Due to the lack of specifics, this regex would do what you ask:

echo preg_replace("/[a-z0-9]+/", '1', $text);

 

It replaces all alphanumeric characters with the number 1

1 = (1 ^ 1 - 1 ^ 1 + 1 ^ 1 + 1 ^ 1) / 1
1 = ((1 * 1 * 1) + SQR(D)) / (1 * (1 ^ 1 + 1 ^ 1))

 

However, I made it only replace lowercase letters since you want to keep the functions like SQR() which you have in caps.

 

I can't believe that is what you want, but again, you are not giving enough information.

Link to comment
Share on other sites

Iam sory...

 

All i want is to replace c. yb, D, xb, yb, with numbers. I dont know if variable c is a lowercase letter or C, because user insert this code.

If you see my example in previous post, you will understand how i wanna use it!

 

 ((2 * c * yb) + SQR(D)) / (2 * (xb ^ 2 + yb ^ 2))

 

The SQR(D) is the biggest problem, and this is what i cant replace. Maybe SQR can used in pattern?

The second problem is that can be yb and yb1 or yba ...

 

As I say i think i should use preg_replace to replace all alphanumeric ( [a-zA-Z0-9] ) between math symbols or parentheses ( "+-/*^()" ) and I dont want to replace the SQR, ATN, ABS, pi and e (like [^ABS|SQR|ATN|ABS|pi|e] ).

Link to comment
Share on other sites

You still haven't stated what "number" or numbers are being used for the replacements.

 

Also, I don't think the logic of "...to replace all alphanumeric ( [a-zA-Z0-9] ) between math symbols or parentheses ( "+-/*^()" ) ..." will work.

 

In your example above SQR IS between parenthesis:

((2 * c * yb) + SQR(D)) / (2 * (xb ^ 2 + yb ^ 2))

 

So, what rule would you use to differentiate "SQR" from "yb". It could be made to not replace is the value preceeds an opening paren, but if this is user entered code you are going to be really struggling for a solution. Do, you have a known list of ALL the math functions that should not be replaced? It might be easier to do this through mutliple steps instead of one preg_replace(). For example, 1) replace all the functions with a temporary value, 2) then replace all the letters and numbers, 3) lastly, replace the temp values back to the functions.

Link to comment
Share on other sites

Here it is an example using the str_replace:

 

<?php
$variables['x'] = 60;
$variables['y'] = 30;
$variables['xb'] = 100;
$variables['yb'] = 50;
$variables['D'] = 25;

$example1 = "(x ^ 2 + y ^ 2) / 2";
$example2 = "((2 * yb) + SQR(D)) / (2 * (xb ^ 2 + yb ^ 2))";

foreach($variables as $key => $value) {
$example1 = str_replace($key, $value, $example1);
$example2 = str_replace($key, $value, $example2);
}

// Echo: (60 ^ 2 + 30 ^ 2) / 2
echo $example1;

// Echo: ((2 * 30b) + SQR(25)) / (2 * (60b ^ 2 + 30b ^ 2))
echo $example2;
?>

 

Ofcourse the 2nd example is wrong!

The result should echo this "((2 * 30) + SQR(25)) / (2 * (60 ^ 2 + 30 ^ 2))"

 

The list of ALL the math functions that should not be replaced is this "sin, cos,tan, asin, acos, SQR, ATN, atan, ABS, exp, log, ln, mod, div pi, e".

Multiply steps sounds bad, but i really wana see your thought (if you can spend your time to this).

Link to comment
Share on other sites

I feel you are mis-interpreting what Regex is good at. Given the example that you have using str_replace it can't really be done using a single Regex pattern. The closest you could get is using a method very similar to what you have like so...

 

$patterns = array('#\bx\b#i', '#\by\b#i', '#\bxb\b#i', '#\byb\b#i', '#\bD\b#i');
$replacements = array(60, 30, 100, 50, 25);

$output = preg_replace($patterns, $replacements, $input);

Obviously each individual pattern can be tweaked slightly.

 

Link to comment
Share on other sites

Your example has a list of variables to be replaced and the values to use. I will ask again, how are you determining what values to use for the replacements. Are you expecting this to be automated? If you "know" the values to be replaced, then you can do something like this:

 

$replacements = array(
    'c'  => 60,
    'yb'  => 30,
    'xb' => 100,
    'ya1' => 50,
    'D'  => 25
);

function formatReplacements($val)
{
    return "#\b{$val}\b#";
}

$find = array_map('formatReplacements', array_keys($replacements));

$input = 'c = (l1 ^ 2 - l2 ^ 2 + xb ^ 2 + yb ^ 2) / 2
ya1 = ((2 * c * yb) + SQR(D)) / (2 * (xb ^ 2 + yb ^ 2))';

echo preg_replace($find, $replacements, $input);

 

Output:

60 = (l1 ^ 2 - l2 ^ 2 + 100 ^ 2 + 30 ^ 2) / 2
50 = ((2 * 60 * 30) + SQR(25)) / (2 * (100 ^ 2 + 30 ^ 2))

Link to comment
Share on other sites

User inserts names and values of variables. User uploads an .srt file with simple C lang with mathematical actions and script returns an array with variables' names and values.

 

For example:

input can be:

l1 = 100
l2 = 200
x = SQR(l1^2 + l2^2)

 

and output should be:

Array ( [l1] => 100 [l2] => 200 [xb] => 226,7)

 

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.