Sunday 10 April 2016

Php helper functions

Below are some usefull functions which you can use in your projects in any PHP framwork.
 
1. Below function will generate string as every string come with single quotes like ('Php','vTiger','Yii','Lavavel').
function in_clause($string){
   return "in ('" . str_replace(",", "','", $string) . "') ";
}
2. To count digit in a given numer.
function count_digit($number) {
    $digit = 0;
    do {
        $number /= 10;      //$number = $number / 10;
        $number = intval($number);
        $digit++;
    } while ($number != 0);
    return $digit;
}
2. To format an array as string which is need for running IN clause.
function format_in_clause($array) {
    if (isset($array)) {
        $realArray = explode(',', $array);
        if (is_array($realArray)) {
            $stringForIn = "'" . implode("','", $realArray) . "'";
            return $stringForIn;
        } 
    } else {
        return NULL;
    }
}
3. To converting an array to string.
function get_comma_seperator($array) {
    if (is_array($array)) {
        return $str = implode(",", $array);
    } else if (is_object($array)) {
        return implode(",", (array) $array);
    } else {
        return $array;
    }
}
4. To find a string contain comma in it or not.
function find_comma($string) {
    if (strpos($string, ",") !== false) {
        return 1;
    } else {
        return 0;
    }
}
5. To find a url contain http:// , https:// in it or not.
function check_http($url) {
    if (strpos($url, "http://") !== false) {
        return 1;
    } else {
        if (strpos($url, "https://") !== false) {
            return 1;
        } else {
            return 0;
        }
    }
}
6. To generate unique ids using mysql.
function gen_uuid() {
        global $adb;
        $order_id = NULL;
        $res = $adb->pquery("SELECT uuid() As order_id", array());
        $norows = $adb->num_rows($res);
        if ($norows > 0) {
            $order_id = $adb->query_result($res, 0, 'order_id');
            $order_id = str_replace('-', '', $order_id);
        }
        return $order_id;
    }