Home  >  Article  >  Backend Development  >  Digital currency type verification function in php

Digital currency type verification function in php

高洛峰
高洛峰Original
2016-11-29 15:43:401215browse

function is_number( $str )  
{  
        if ( substr( $str, 0, 1 ) == "-" )  
        {  
                $str = substr( $str, 1 );  
        }  
        $length = strlen( $str );  
        $i = 0;  
        for ( ;    $i < $length;    ++$i    )  
        {  
                $ascii_value = ord( substr( $str, $i, 1 ) );  
                if ( 48 <= $ascii_value && $ascii_value <= 57 )  
                {  
                        continue;  
                }  
                return FALSE;  
        }  
        if ( $str != "0" )  
        {  
                $str = intval( $str );  
                if ( $str == 0 )  
                {  
                        return FALSE;  
                }  
        }  
        return TRUE;  
} 
 
function is_decimal( $str )  
{  
        if ( substr( $str, 0, 1 ) == "-" )  
        {  
                $str = substr( $str, 1 );  
        }  
        $length = strlen( $str );  
        $i = 0;  
        for ( ;    $i < $length;    ++$i    )  
        {  
                $ascii_value = ord( substr( $str, $i, 1 ) );  
                if ( 0 < $i && $ascii_value == 46 || 48 <= $ascii_value && $ascii_value <= 57 )  
                {  
                        continue;  
                }  
                return FALSE;  
        }  
        return TRUE;  
} 
 
function is_money( $str )  
{  
        $dot_pos = strpos( $str, "." );  
        if ( !$dot_pos )  
        {  
                return FALSE;  
        }  
        $str1 = substr( $str, 0, $dot_pos );  
        if ( 14 < strlen( $str1 ) )  
        {  
                return FALSE;  
        }  
        if ( !is_number( $str1 ) )  
        {  
                return FALSE;  
        }  
        $str2 = substr( $str, $dot_pos + 1, strlen( $str ) - $dot_pos );  
        if ( strlen( $str2 ) != 2 )  
        {  
                return FALSE;  
        }  
        if ( !is_number( $str2 ) )  
        {  
                return FALSE;  
        }  
        return TRUE;  
} 
 
function is_money_len( $str, $int_len, $dot_len )  
{  
        $dot_pos = strpos( $str, "." );  
        if ( !$dot_pos )  
        {  
                return FALSE;  
        }  
        $str1 = substr( $str, 0, $dot_pos );  
        if ( $int_len < strlen( $str1 ) )  
        {  
                return FALSE;  
        }  
        if ( !is_number( $str1 ) )  
        {  
                return FALSE;  
        }  
        $str2 = substr( $str, $dot_pos + 1, strlen( $str ) - $dot_pos );  
        if ( strlen( $str2 ) != $dot_len )  
        {  
                return FALSE;  
        }  
        if ( !is_number( $str2 ) )  
        {  
                return FALSE;  
        }  
        return TRUE;  
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn