Replace character by PHP function

Asked

Viewed 294 times

1

I have to make a calculation based on two things:

1 - The value that the user informs

2 - The basic account I have registered in the bank

I have the base account: 5149.3074*{{INPUT}} (0.0001)-5129.6906

Where is {{INPUT}} is the value that the user informs in the input, until then I can replace {{INPUT}} by the value with the function str_replace. The biggest problem is in "^" which means "Power" and for that PHP needs a function to calculate. What I need is to take the value that is after the "^" between parentheses.

How can I do that ?

4 answers

3


You can use the substring php, thus:

$conta = "5149.3074*{{INPUT}}^(0.0001)-5129.6906";
$primeiro = strpos($conta, "(");
$segundo = strpos($conta, ")");
$valor = substr($conta, $primeiro + 1, $segundo - $primeiro - 1);

See working on Ideone.

Or do with the Regex:

$conta = "5149.3074*{{INPUT}}^(0.0001)-5129.6906";
preg_match("/\(([^\]]*)\)/", $conta, $valor);

See working on Ideone.

  • Thank you @Francisco .. Your Regex worked perfectly. Could you explain to me in detail what each item of your regex does ? (only if you really succeed)

  • There is a site that generates the regex for you, just know the basics. PHP Live Regex

  • could you help me one more time? I have one last calculation that is like this (({{price}}+625.5175)/637.3620) 5963.7532 and the power is now at the end and without parentheses, which would be the Regex to take only the values ?

  • Would look like this: preg_match("/[\^](.*)\./", $conta, $valor);

  • 1

    until it works only that it does not take the decimals, IE, it takes only 5963 and not 5963.7532

  • Ahh, I thought the point was multiplying kkk, brisei. So, do preg_match("/[\^](.*)/", $conta, $valor);.

  • It worked, now only gave a blow the first you helped me because you had to have the parentheses to sequence the right calculation. The formula is: (5149.3074({{INPUT}} (0.0001)))-5129.6906* And Regex is bringing in some parentheses.

  • To take the most important (counted with the parentheses) do so: preg_match("/\(([0-9.]+)\)/", $conta, $valor); If this account always changes, I recommend that you make an algorithm to suit.

Show 3 more comments

1

Using a simpler REGEX:

\(([0-9.]+)\)

This will allow any character between 0 and 9 (0-9) and points (.), in this case he would allow a 0...123. But I wouldn’t take any () additional. However, if you only want a single point (or none) you can use:

\(([0-9]+?(\.[0-9]+)?)\)

In general he can get all the numbers before ., if any, and also the figures after the ., if there is also.

inserir a descrição da imagem aqui

As \( and \. are to escape once the () and the . is used in REGEX for other cases.


Testing:

5149.3074*{{INPUT}}^(0.0001)-5129.6906 =>               0.0001
(5149.3074*({{INPUT}}^(0.0001)))-5129.6906 =>           0.0001
(5149.3074*({{INPUT}}^(((((((((0.0001)))))-5129.6906 => 0.0001
5149.3074*{{INPUT}}^(1)-5129.6906 =>                    1
5149.3074*{{INPUT}}^((((((1))))))-5129.6906 =>          1
5149.3074*{{INPUT}}^((((1)))))-5129.6906 =>             1
5149.3074*{{INPUT}}^((((1....123)))))-5129.6906 =>      Não encontrado

Test this

0

Regular expression: ideone

$conta = "5149.3074*{{INPUT}}^(0.0001)-5129.6906";
preg_match('^\((.*)\)^', $conta, $match);
print $match[1];

The circumflex marks the beginning of a line. ^\( means that the beginning should be by parentesis (, the backslash serves to escape the ( since it has a function in REGEX. Parentheses define a group, and its contents can be seen as a block in the expression (...) - indicates the beginning and end of a group respectively.

.   ponto       um caractere qualquer

*   asterisco   zero, um ou mais 

that is, within parentheses we can have as many characters as there are.

About function preg_match()

  1. The first parameter is the regular expression. ^\((.*)\)^
  2. The second parameter is the string where we can find the expression. $conta
  3. The third parameter is an array that will store the term you married ($matches).

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.