Category Archives: PHP

Route CodeIgniter Controllers In Multi Level Subfolder

I was working on a project which actually requires to handle multi level subfolders for codeigniter. Usually codeigniter can handle only one subfolder. But this reduce capability of it and I am always not like to change codes in CI core. So i did some research on this found a simple solution by custom routing to extend CI core.

  1. Create a file Named “MY_Router.php” in CI application/libraries folder.
  2. Copy paste following script in it.
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    /**
     * CodeZone.in
     * Author: Dineshkumar
     * Contact : hi@codezone.in
     * Add functionality to read into more than one sub-folder of controller
     * Compatible with Codeigniter 3.x
     *
     */
     
    Class MY_Router extends CI_Router
    {
        Function MY_Router()
        {
            parent::__construct();
        }
    
        function _validate_request($segments)
        {
    
           if (file_exists(APPPATH.'controllers/'.$segments[0].".php"))
            {
                return $segments;
            }
    
            if (is_dir(APPPATH.'controllers/'.$segments[0]))
            {
                $this->set_directory($segments[0]);
                $segments = array_slice($segments, 1);
    
                /* ----------- CHECK SUB FOLDER STARTS ------------ */
    
                while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
                {
                if (substr($this->directory, -1, 1) == '/')
                    $this->directory = $this->directory . $segments[0];
                else
                    $this->directory = $this->directory . '/' . $segments[0];
    
                $segments = array_slice($segments, 1);
                }
    
                if (substr($this->directory, -1, 1) != '/')
                    $this->directory = $this->directory . '/';
    
                /* ----------- CHECK SUB FOLDER ENDS ------------ */
    
                if (count($segments) > 0)
                {
    
                    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/'.$segments[0].".php"))
                    {
                        show_404($this->fetch_directory().$segments[0]);
                    }
                }
                else
                {
                    $this->set_class($this->default_controller);
                    $this->set_method('index');
    
                    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().'/' .$this->default_controller.".php"))
                    {
                        $this->directory = '';
                        return array();
                    }
    
                }
    
                return $segments;
            }
    
            show_404($segments[0]);
        }
    }
  3. Don’t look for more steps its over. 😛

Just your codeigniter framework capability is extended to handle

String to Hex & Hex to String Conversion in PHP

Hi Guys I just gone through a situation need to convert string to Hex but unfortunatly not found any Pre-build function in PHP but don’t worry today I would like to share you a function which solve the purpose in either way

String to Hex Function

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function string2hex($string){
    $hex = '';
    for ($i=0; $i<strlen($string); $i++){
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return strToUpper($hex);
}

Hex to String Function

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function hex2string($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

Usage:

Include function in your PHP script and call them with required parameters to get your converted output.

Happy Coding….

ASCII to HEX Convertion in PHP

Hi guys.. I was searching for function to convert ASCII to HEX in PHP accidentally there is no pre-build function so today I am going to share you a function which will do this conversation.

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function ascii2hex($ascii) {
  $hex = '';
  for ($i = 0; $i < strlen($ascii); $i++) {
    $byte = strtoupper(dechex(ord($ascii{$i})));
    $byte = str_repeat('0', 2 - strlen($byte)).$byte;
    $hex.=$byte." ";
  }
  return $hex;
}

Happy coding….