FILTER_SANITIZE_NUMBER_FLOAT constant in PHP

PHPProgrammingServer Side Programming

The FILTER_SANITIZE_NUMBER_FLOAT constant deletes all illegal characters from a float number.

Flags

  • FILTER_FLAG_ALLOW_FRACTION − Allows fraction separator

  • FILTER_FLAG_ALLOW_THOUSAND − Allows thousand separator

  • FILTER_FLAG_ALLOW_SCIENTIFIC − Allows scientific notation

Return

The FILTER_SANITIZE_NUMBER_FLOAT constant does not return anything.

Example

The following is an example that use FILTER_FLAG_ALLOW_FRACTION flag.

 Live Demo

<?php
   $var = "3-1f+2.56p";
   var_dump(filter_var($var, FILTER_SANITIZE_NUMBER_FLOAT,
   FILTER_FLAG_ALLOW_FRACTION));
?>

Output

The following is the output.

string(8) "3-1+2.56"

Let us see another example. Here, FILTER_FLAG_ALLOW_THOUSAND flag is used −

Example

 Live Demo

<?php
   $var = "1-4f+25,6p";
   var_dump(filter_var($var, FILTER_SANITIZE_NUMBER_FLOAT,
   FILTER_FLAG_ALLOW_THOUSAND));
?>

Output

Here is the output.

string(8) "1-4+25,6"
raja
Published on 04-Dec-2018 08:18:29
Advertisements