Few useful php functions in day today life..!

Few useful php functions in day today life..!


/*This function strip html tags you want ,
Inside text area. In database those tags will not inserted
*/
function strip_only($str) {

            $tags =
            array('html','body','style','div',
            'table','tr','td','script','img');
            if(!is_array($tags)) {
            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
            if(end($tags) == '') array_pop($tags);
            }
            foreach($tags as $tag)
            $str = preg_replace('#]*>#is', '', $str);
            return $str;
    }
 /*Returns '-------' if variable passed is null, blank, or undefined.*/
    function is_blank($val,$ret_val=null){
        if(!isset($val)){
            if($ret_val){
                $str = $ret_val;
            }
            else{
                $str = '-------';
            }
        }
        elseif(empty($val)){
            if($ret_val){
                $str = $ret_val;
            }
            else{
                $str = '-------';
            }
        }
        else{
            $str = $val;
        }

        return $str;
    }
    /*Returns '-------' if variable passed is null, blank, or undefined.*/
/*Date Functions*/
    function custom_date_format($date_time,$date_time_format_from = 'Y-m-d H:i:s',$date_time_format_to = 'm-d-Y'){
        $date_time_obj = date_create_from_format("$date_time_format_from", $date_time);

        # Now set a new date using date_format();
        $new_date_time = date_format( $date_time_obj, "$date_time_format_to");
        return $new_date_time;
    }
    /*Date Functions*/
function restrict_details($string,$val_want,$strip_tags=null)
    {
        $strw = '';
        $len = strlen($string);
       // echo "".$len."--".$string."--".$val_want;

        if($strip_tags)//remove html if exist pass 1 or 0 respectively
        {
            $string = strip_tags($string);
        }

        if($len > $val_want)
        {
            $len_sub=substr($string,0,$val_want);
            $final=$len_sub."....";
            $strw .= $final;
        }
        else
        {
            $strw.= $string;
        }

        return $strw;

    }

No Comments

(Required)
(Required, will not be published)
*

© Open Source Rocks