Hilarious Programming Jargon

Today i found a funny article on www.businessinsider.com about the programming jargon:

Heisenbug: A play on “Heisenberg,” a principle in quantum mechanics, a Heisenbug is a bug that disappears or alters its characteristics when an attempt is made to study it.

Hydra: A bug that, when an attempt to fix is made, introduces two new bugs. It’s a bug that cannot be fixed.

Mad girlfriend bug: When you see something strange is happening, but the software is telling you everything is fine.

Higgs Bugson: Another bug based on a physics phenomenon, a Higgs Bugson is a bug that’s hypothetically predicted to exist based on other conditions, but is difficult to produce.

Read the full article here

PHP Credit Card Validator – Class

If you ever need to validate credit card numbers, here is a simple validator class. Of course you can use it as simple as just defining validation rules in one array and use the php biult-in preg_match function to validate. But for the sake of good OOP practice, we need a class ๐Ÿ™‚ . Well, i hope that this will help you. So, first we create the validator class:

setCreditCardRules();
    }

    /**
     * Set the rules for credit card validation
     *
     * @author  [email protected]
     * @since   May 2, 2012
     */
    private  function setCreditCardRules()
    {
        $this->rules['creditCard'] = array(
            'amex'     => '/^3[4|7]\\d{13}$/',
            'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
            'diners'   => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
            'disc'     => '/^(?:6011|650\\d)\\d{12}$/',
            'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
            'enroute'  => '/^2(?:014|149)\\d{11}$/',
            'jcb'      => '/^(3\\d{4}|2100|1800)\\d{11}$/',
            'maestro'  => '/^(?:5020|6\\d{3})\\d{12}$/',
            'mc'       => '/^5[1-5]\\d{14}$/',
            'solo'     => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
            'switch'   => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})' .
                          '\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4]' .
                          '[0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
            'visa'     => '/^4\\d{12}(\\d{3})?$/',
            'voyager'  => '/^8699[0-9]{11}$/',
            'fast'     => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3' .
                          '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'
            );
    }

    /**
     * Get available rules for credit card validation. In case you need
     * them to pass to another object(s) and you don't want to use
     * the validator
     *
     * @author  [email protected]
     * @since   May 2, 2012
     * @return  array
     */
    public function getCreditCardRules()
    {
        return $this->rules['creditCard'];
    }

    /**
     * The validator
     *
     * @author  [email protected]
     * @since   May 2, 2012
     * @param   string $cardType Card type as defined in setCreditCardRules()
     * @param   numeric $cardNumber Credit card number
     * @throws  \Exception
     */
    public function isValid($cardType, $cardNumber)
    {
        if(!isset($this->rules['creditCard'][$cardType]))
        {
            throw new \Exception('Invalid card type');
        }

        return preg_match($this->rules['creditCard'][$cardType], $cardNumber);
    }

}
?>

Easy, no ? Using it is very, very simple:

isValid($cardType, $cardNumber))
    {
        echo 'Credit card number is valid';
    }
    else
    {
        echo 'Invalid credit card number';
    }
?>

Hope it is useful for you. Cheers !

Symfony 1.4 validate form fields depending on the other fields value

If you are working with Symfony, you might need from time to time validate a field, depending on the other filed value. There are a few ways to accomplish this. One quick method is to override the bind function:

validatorSchema["secondAddress"]->setOption('required', false);
  }

  return parent::bind($taintedValues, $taintedFiles);
}
?>

So, if user is sending the first address, we don’t need the second address. Of course this is just a dummy example ๐Ÿ™‚

Convert multidimensional array to object – PHP

As you may already know, you can convert an array into an object simple like this:

 'John', 'last_name' => 'Doe');
    $object = (object) $array;
?>

Then you call the object:

first_name;
?>

The problem is that you can’t do this with multidimensional array. So, for this, i’ve found the solution:

 0) {
      foreach ($array as $name=>$value) {
         $name = strtolower(trim($name));
         if (!empty($name)) {
            $object->$name = arrayToObject($value);
         }
      }
      return $object;
    }
    else {
      return FALSE;
    }
}

// Now we can test the method
$array = array(
    'f_name' => 'Rada',
    'l_name' => 'Calin',
    'age' => 26,
    'parents' => array(
        'mom' => array(
            'f_name' => 'Rada',
            'l_name' => 'Daniela',
            'age' => 45,
        ),
        'dad' => array(
            'f_name' => 'Rada',
            'l_name' => 'Ioan',
            'age' => 46,
        )
    )
);

// Convert the array to object
$user = arrayToObject($array);

// Example output
echo $user->parents->mom->l_name;
?>

Hope this is helpful ๐Ÿ˜‰

Ubuntu server network problems โ€“ network down after reboot

Iโ€™ve experimented a strange situations a few days ago. After a few updates, my VPS became unreachable. Ifup command was throwing me errors โ€“ directory โ€œ/var/run/networkโ€ does not exists. After a few hours of researches, it seems that this is a problem that was solved back in 2008. Unfortunately, for some reasons โ€“ most probably because of updates the problem is back and i have no idea how to fix it โ€“ iโ€™m not an expert. But, i tried the following thing that itโ€™s working for me.

1. Create a .sh file with the following contents. Letโ€™s name it network_check.sh

[prettify class=”bash”]

if [ ! -d “/var/run/network” ]; then

mkdir -p /var/run/network

sudo /etc/init.d/networking restart

sudo /etc/init.d/ssh restart

fi

[/prettify]

2. Create a cron to execute the script

[prettify class=”bash”]

*/1 * * * * root sh /PATH-TO-SCRIPT/network_check.sh

[/prettify]

As u can see i run the script every minute. You can set as much time as u want, but i like to be up and running immediate after reboot . Hope it helps someone ๐Ÿ™‚