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."
}
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."
}
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
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);