Removing a semicolon by whitespace from formatting - PHP

Asked

Viewed 406 times

0

Hello,

I have the following code to remove the colon and comma:

$valor = str_replace("." , "" , $valor );
$valor = str_replace("," , "" , $valor);

There is how to do this in a single code, without first doing one thing after another?

I searched and couldn’t find a solution to also remove the last two zeros.

Example: 300.000,00 Upshot: 300000

  • If the decimals are non-zero, you will still throw them away without rounding?

  • There will be no values other than zero, as they are real estate values.

  • to remove the semicolon, if I am not mistaken str_replace accepts arrays in the input, then it would be "str_replace([".", ","] , ["", ""] , $value );", I believe it would summarize in a line the removal of these characters.

  • 2

    @Ruggi this code is in error.

  • You’re Right @Gladisonneuzaperosini, corrected.

  • The error disappears, but when I run it gives error. It shows the following: Parse error: syntax error, Unexpected '[', expecting ')'

  • How do I remove the last two zeros?

  • 3

    The code in the Ruggi comment uses array alias, which is available from php 5.5. To make it compatible, use str_replace(array('.', ','), '', $valor);

  • 1

    In the title it says "white space". However, "white space" is different from "empty". I think you want to remove by switching to empty.

  • 1

    actually @daniel-Omine is right, use what Daniel said to replace the score, then remove the last two zeroes, like this: $value = substr($value, 0, -2);

  • 2

    I executed the code without errors, it was like this: $value = "300.000,00"; echo $value." </br>"; $value = str_replace(Array(",", "."), "", $value); echo $value. " </br>"; $value = substr($value, 0, -2 ); echo $value;

  • @Ruggi I use in mysql the float field for coins, that’s correct?

  • @Ruggi whenever I record at the bank he eliminates a few zeros. Like, if I record 300,000 it stores only 300. I’ve lost track of what might be going on.

  • my, so when you write float type in mysql, its decimal separation should be with ".". then I advise you not to take the last two... and add the point in the correct location, you saw the post that marked that are duplicated the answers? tried to record in the bank that way?

Show 9 more comments
No answers

Browser other questions tagged

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