Thursday, January 9, 2020

Codeigniter URL Routing, Suffix & Enable Query String

Wildcards:

We can use two types of wildcards, namely:
  1. :num –  Segment containing only numbers will be matched.
  2. :any – Segment containing only characters will be matched.

$route['(blog/:num)'] = 'tutorial/java/$1';
$route['(blog/:any)'] = 'tutorial/java';

Regular Expression
$route['blog/([a-zA-Z0-9]+)'] = 'tutorial/java';

Saturday, October 12, 2019

JQuery count number of divs with a certain class?


var numItems = $('.item').length;

Get all text field value located in a specific class


$(".test .text-field").each(function() {
    alert($(this).val());
});

Note : test and text-field is class of text field

Friday, June 7, 2019

Remove Menu From Wordpress Admin

add_action('admin_init', 'remove_menu_from_admin');

function remove_menu_from_admin()

    // Remove unnecessary menus
    $menus_to_stay = array(
        // Job Listing
        'edit.php?post_type=job_listing',
    );
    foreach ($GLOBALS['menu'] as $key => $value) {         
        if (!in_array($value[2], $menus_to_stay)) remove_menu_page($value[2]);
    }
}

Thursday, April 18, 2019

PHP Code to Remove Everything Except Number


$mobileNumber = "(111) 2568-5698";

preg_replace('/\D/', '', $mobileNumber);

OR,

preg_replace('/[^0-9]/', '', $mobileNumber);

Monday, April 1, 2019

Encrypt and Decrypt a PHP String

function decEnc($action, $string) {
$output = false;

$encrypt_method = "AES-256-CBC";
$secret_key = 'Secret key';
$secret_iv = 'Secret iv';

// hash
$key = hash('sha256', $secret_key);

// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);

if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}

return $output;
}

Encryption Example:
$string = "hanzala";
decEnc("encrypt", $string);

Decryption  Example:
$string = "eU15RFhTZGN5SWJzTGgrRSs4SW9zQT09";
decEnc("decrypt", $string)

Sunday, March 31, 2019

How to get the value of selected radio button in a group using jQuery

    $(document).ready(function(){
        $("input[type='button']").click(function(){
            var radioValue = $("input[name='gender']:checked").val();
            if(radioValue){
                alert("Your are a - " + radioValue);
            }
        });
       
    });

Allow Number to insert into text box

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

onkeypress="return isNumber(event)"

Friday, March 29, 2019

How to: enable CORS in express.js (node.js)

Express.js is one of the most popular node.js frameworks for serving website or build APIs.

Steps to use CROS in node.js

1. npm install cors
2. Use the following code
   var express = require('express')
   var cors = require('cors')
   var app = express()

app.use(cors())

app.get('/userList', function (req, res, next) {
  res.json({msg: 'Enabled the CROS'})
})

app.listen(80, function () {
  console.log('Enabled CORS listening on port 80')
})