Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Tuesday, 20 September 2016

Compress and Uncompress Data in PHP

Compress string and data in php. Some long strings has to be compressed in-order to pass the data. Use this below function to compress data in php and to uncompress data in php.

Compress Data :
gzcompress($string);

Uncompress Data :
gzuncompress($compressed_string);

Friday, 26 August 2016

Count Words in a String in PHP

In Some cases we need to calculate or count the number of words in a sentence or a long string.
This simple function takes a string as parameter and counts the number of words in the string based on the space in the string.

function countWords($string) {
   $word_array = explode(" ", $string);
   return count($word_array);
}

Friday, 15 July 2016

How to hide notices and warnings in PHP

Method to hide all notices and warnings in php. Use the following code in php.ini file hide all notices and warnings in php.

In php.ini file add the below code

error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING

Wednesday, 13 July 2016

PHP Function to Convert Array to Object

Easy php function to convert php array to php object. Php objects are easy to access and get data than array.
function convertToObject(array $array) {
    foreach($array as $key => $value) {
            if (is_array($value)) {
                $array[$key] = convertToObject($value);
            }
     }    
     return (object) $array; 

}

Tuesday, 12 July 2016

PHP Function to compress image in lossless quality

Efficient php function to compress image quality without any loss in quality and clarity,
function compressImageQuality($source_path, $destination_path , $quality) {
    $info = getimagesize($source_path);
    if ($info['mime'] == 'image/jpeg') {
        $image = imagecreatefromjpeg($source_path);
    }
    elseif ($info['mime'] == 'image/gif') {
        $image = imagecreatefromgif($source_path);
    }
    elseif ($info['mime'] == 'image/png') {
        $image = imagecreatefrompng($source_path);
    }
    else { die('File format not supported'); }

    imagejpeg($image, $destination_path, $quality);
    return $destination_path;
}


This function will read jpg/gif/png image and compress it and save to jpg format. The imagejpeg  function compresses the image and stores as jpeg image.

PHP Function to download a file from URL

Simple php function to download files like pdf, xml, doc and images from a url and save to given path in server
function download_remote($source_url , $destination_path) {
    $file = fopen( $destination_path , 'w+');
    $file_handle = fopen($source_url , "rb");
    while (!feof($file_handle)) {
        $file_contents = fread($file_handle, 8192);
        fwrite($file , $file_contents);
    }
    fclose($file_handle);
    fclose($file);
}
In this $source_url holds the path of file to download and $destination_path holds the path to save file.

Monday, 11 July 2016

PHP function to parse text to hyperlinks

Following function is used to parse text and convert the urls into links, in addition it converts mail id to mailto links. This simple function basically functions on regular expressions.

function parseTxt2Links($fulltxt) {
    $fulltxt = str_replace('www.', 'http://www.', $fulltxt);
    $fulltxt = preg_replace('|http://([a-zA-Z0-9-./]+)|', '<a href="http://$1">$1</a>', $fulltxt);
    $fulltxt = preg_replace('/(([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6})/', '<a href="mailto:$1">$1</a>', $fulltxt);
    return $fulltxt;
}

Sunday, 10 July 2016

PHP function to redirect to previous page

Easy way to redirect a webpage to previous page in php
function redirectToPreviousPage() {
    header("Location: {$_SERVER['HTTP_REFERER']}");
    exit;
}
In this $_SERVER is a superglobal variable, in which HTTP_REFERER holds the address of the page which refered this page.

Thursday, 21 April 2016

PHP function to get source of webpage

Simple php function to get and display source code of a webpage.
function getSourceCode($pageurl) {
     $lines = file($pageurl);
     $output = "";
     foreach ($lines as $line_num => $line) {
         // loop thru each line and prepend line numbers
         $output.= "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br>\n";
     }
}

Wednesday, 20 April 2016

Php function to validate email address

PHP function to validate Email Address 

Simple php function to validate email address. Helps in validating email address in php.

function isValidEmail($email) {
    if(preg_match("/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) > 0)
        return true;
    else
        return false;
}

Monday, 18 April 2016

PHP function to convert Multidimensional Arrays to stdClass Objects

PHP function to convert Multidimensional Arrays to stdClass Objects

<?php
    function array2object($myarray) {
        if (is_array($myarray)) {
            return (object) array_map(__FUNCTION__, $myarray);
        }
        else {
            return $myarray;
        }
    }
?>

PHP function to convert stdClass Objects to Multidimensional Arrays

PHP function to convert stdClass Objects to Multidimensional Arrays

<?php
    function object2array($myobj) {
        if (is_object($myobj)) {
            $myobj = get_object_vars($myobj);
        }
        if (is_array($myobj)) {
            return array_map(__FUNCTION__, $myobj);
        }
        else {
            return $myobj;
        }
    }
?>

Friday, 15 April 2016

PHP Predefined Constants

PHP Predefined Constants

Php in default has some predefined constants. This post explains about some major PHP magic constants.

  • __DIR__ – The directory of the file.
  • __FILE__ – The full path and filename of the file.
  • __CLASS__ – The class name.
  • __FUNCTION__ – The function name.
  • __METHOD__ – The class method name.
  • __LINE__ – The current line number of the file.
  • __NAMESPACE__ – The name of the current namespace

Tuesday, 12 April 2016

Add PHP ordinal suffix to Numbers

Add PHP ordinal suffix to Numbers

Function to add st, nd, rd and th to numbers in php

function numberSuffix($number) {
        if(!in_array(($number % 100),array(11,12,13))) {
            switch ($number % 10) {
              case 1: return $number.'st';
              case 2: return $number.'nd';
              case 3: return $number.'rd';
            }
        }
        return $number.'th';
}

Thursday, 7 April 2016

Right and Left Trim in php

Right and Left Trim in php

Right Trim:

To right trim a string use "rtrim" function in php
$path = "/test-string/"
echo $path = rtrim($path,"/"); 

Output:
/test-string


Left Trim:

To left trim a string use "ltrim" function in php
$path = "/test-string/"
echo $path = ltrim($path,"/"); 

Output:
test-string/