0

Hi I'm trying to find out where a symbol has a minus or a decimal point but can't seem to figure out how to do it.

Here is what I have:

  if (strpos($dec, '.')){
        echo "A decimal occured."
      }
Tim
  • 5
  • 5
  • Possible duplicate of [How to check if a string contains a specific word in PHP?](http://stackoverflow.com/questions/4366730/how-to-check-if-a-string-contains-a-specific-word-in-php) – Tim Oct 17 '16 at 18:19

2 Answers2

1

PHP's strpos will return the position of the minus or decimal point, if found, or false if not found.

$spaces = '2,223.00';
$pos = strpos($spaces, '.');

if (false === $pos) {
    echo 'No decimal found';
} else {
    echo 'Decimal found at ' . $pos;
}

Note that I used === to check the result.

This is explained in detail in the manual at https://secure.php.net/manual/en/function.strpos.php

Andrew McCombe
  • 1,603
  • 1
  • 12
  • 13
0
if(preg_match ( ".", $spaces )){
  echo "A decimal occured."
}

if(preg_match ( "-", $spaces )){
  echo "A minus occured."
}

Or comparing bought:

if(preg_match ( "/(\.|-)/" , $spaces )){
  echo "Minus or decimal";
}

You need to be sure that $spaces is a string

strval($spaces);