Monday, November 14, 2011

Validate the Credit Card Number

/**
 * Check that Credit Card number is valid or not.
 *
 * @author    Junaid Atari <mj.atari@gmail.com>
 * @version   1.1
 * @param     array    $number    Credit Card number
 * @param     string   $cctype    Card Type to check.
 * @return    bool     TRUE on valid | else FALSE
 */

function isCCNumberValid $number$cctype )
{
    if ( !
is_string $number)
         || !
trim $number ) )
             return 
false;
      
    
$ccs = array (

        /* Patterns written by Junaid Atari */

        // Master Card
        
'master' => '/^(5[1-5]{1})(\d{14})$/',

        
// American Express 
        'amex' => '/^3(4|7)\d{13}$/',
        
        // Discover Card 
        'discover' => '/^6(011|22|4|5)(\d{12}|\d{13}|\d{14})$/'
        

        // Visa Card 
        'visa' => '/^4(\d{12}|\d{15})$/',

        
// Dinners Club        
        'dinners' => '/^(305|36|38|54|55)(\d{12}|\d{14})$/',

        // Carte Blanche         
        'carte' => '/^(30[0-5]{1})(\d{11})$/'
        
        // enRoute 
        'enroute' => '/^2(014|149)\d{11}$/'
        

        // Laser
        'laser' => '/^6(304|706|771|709)(\d{12}|\d{13}|\d{14}'.
                   '|d{\15})$/'
        
        // Visa Electron 
        'visaelec' => '/^4(17500|917|913|844|508)(\d{10}|\d{12})$/'
    
);

    if ( !
is_string $cctype )
         || !
array_key_existsstrtolower ($cctype) , $ccs ) )
             return 
false;

    return (bool) 
preg_match $ccs[$cctype], $number );

Remove Tags from String

/**
 * Remove the tags list from given string.
 *
 * @author     Junaid Atari <mj.atari@gmail.com
>
 * @version    1.1
 * @param      array     $tagsList    Url of page
 * @param      string    $contents    Variable which need to be clean.
 * @return     string    Will direct update the variable in memory.
 */

function removeTags ( array $tagsList$contents )
{
    foreach ( 
$tagsList as $tag )
    {
        
$tag trim preg_quote $tag'/' ) );
        $contents (string) $contents;
         
        if ( 
$tag != '' )   
            
//** Pattern copyright 2011 Junaid Atari
            
return preg_replace '/((<'.$tag.'.*(\/>|>.*<\/'.$tag.
                                  '>)))(\s)?/imsU','', $contents );
        return $contents;
    }

Beautify JSON string

/**
 * pretty_json ()
 * beautify the JSON string
 *
 * Use 'indent' option to select indentation string - by default it's a tab
 *
 * @param    string    $json         Original JSON string
 * @param    array    $options     Encoding options
 * @return    string    Beautified JSON string
 */

function pretty_json $json$options = array() )
{
    
$tokens preg_split ('|([\{\}\]\[,])|',
              $json, -1PREG_SPLIT_DELIM_CAPTURE);
    
$result "";
    
$indent 0;

    
$ind "\t";

    if ( isset ( 
$options['indent'] ) )
        $ind $options['indent'];

    foreach ( 
$tokens as $token )
    {
        if ( 
$token == "" ) continue;

        
$prefix str_repeat ($ind$indent); 

        if ( 
$token == "{"
             
|| $token == "[" )
        {
            
$indent++;
            if ( 
$result != ""
                 
&& $result[strlen ($result) - 1] == "\n" )
                    
$result .= $prefix;
              
            
$result .= "$token\n";
        }
        else if ( 
$token == "}"
                  
|| $token == "]" )
        {
            
$indent--;
            
$prefix str_repeat $ind$indent );
            
$result .= "\n$prefix$token";
        }
        else if (
$token == ","$result .= "$token\n";
        else 
$result .= $prefix.$token;
    }
  
    return 
$result;