Tuesday, October 27, 2015

Sunday, October 25, 2015

Codeigniter - The best practice in _remap() function

Instead of using like this:

function _remap($method)
{
         $array = array('method1','method2','method3','method4','method5','method6','method7',...);
         in_array($method, $array) ? $this->method() : $this->index()
}


to


function _remap($method)
    {
        $arr_raw_method_registered = get_class_methods(__CLASS__);
        $arr_remove_element = array('get_instance');
        $arr_method_registered = array_filter($arr_raw_method_registered, function($item)use($arr_remove_element) 
        {
            if (in_array($item, $arr_remove_element) || $item[0] == '_'):
                return false; 
            else:
                return true;
            endif;
        });
        
        in_array($method, $arr_method_registered) ? $this->$method() : $this->index();
    }

The function covers all defined function on that class.



Monday, October 19, 2015

Regex to Catch image in img tag

<img .*?src="[^"]*\/\K([^"]*?\.(?:jpeg|jpg|bmp|gif|png))

My Name in Regex!

^([\+?I?])([\+?n?])([\+?d?])([\+?e?])([\+?r?])([\+?a?])(\d{0,9})$

Useful websites to learn Regex

Basic Tutorial
  1. http://www.macronimous.com/resources/writing-regular-expression-with-php.asp
Regex Website Tester
  1. https://regex101.com/

Regex to Valid Phone Number (Malaysia)

     function valid_phone_num($str)
     {
     if (!preg_match("/^(\+?6?01)[0|1|2|3|4|6|7|8|9]\-*[0-9]{7,8}$/", $str)):
       $this->form_validation->set_message('valid_phone_num', 'Please insert a valid phone number format. Example : 0191234567.');
       return FALSE;
     else:
       return TRUE;
    endif;
    }

Wednesday, October 14, 2015

Codeginter RollBack

https://ellislab.com/codeigniter/user-guide/database/transactions.html

Wednesday, October 7, 2015

WebcamJS

https://github.com/jhuckaby/webcamjs