Showing posts with label PHP Functions. Show all posts
Showing posts with label PHP Functions. Show all posts

Sunday 30 November 2014

PHP file_get_contents Function - Reads entire file into a string

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
Reads entire file into a string
This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.

<?php
$homepage 
file_get_contents('http://www.example.com/');
echo 
$homepage;?>

5.1.0 Added the offset and maxlen parameters. 5.0.0 Added context support.

PHP mail attachment - sending an attachment with PHP

PHP utf8_encode Function - Encodes an ISO-8859-1 string to UTF-8

string utf8_encode ( string $data )
Encodes an ISO-8859-1 string to UTF-8
This function encodes the string data to UTF-8, and returns the encoded version. UTF-8 is a standard mechanism used by Unicode for encoding wide character values into a byte stream. UTF-8 is transparent to plain ASCII characters, is self-synchronized (mea

Walk through nested arrays/objects and utf8 encode all strings.



<?php// Usageclass Foo {

    public
$somevar = 'whoop whoop';

}
$structure = array(
    
'object' => (object) array(
        
'entry' => 'hello wörld',
        
'another_array' => array(
            
'string',
            
1234,
            
'another string'
        
)
    ),
    
'string' => 'foo',
    
'foo_object' => new Foo);
utf8_encode_deep($structure);
// $structure is now utf8 encodedprint_r($structure);
// The functionfunction utf8_encode_deep(&$input) {
    if (
is_string($input)) {
        
$input = utf8_encode($input);
    } else if (
is_array($input)) {
        foreach (
$input as &$value) {
            
utf8_encode_deep($value);
        }
        unset(
$value);
    } else if (
is_object($input)) {
        
$vars = array_keys(get_object_vars($input));
        foreach (
$vars as $var) {
           
utf8_encode_deep($input->$var);
        }
    }
}
?>

1 7 0bbbbbbb 2 11 110bbbbb 10bbbbbb 3 16 1110bbbb 10bbbbbb 10bbbbbb 4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb

Corrigindo erros de acentuação no PHP

Saturday 29 November 2014

com_create_guid Generate a globally unique identifier

string com_create_guid ( void )
Generate a globally unique identifier (GUID)
Generates a Globally Unique Identifier (GUID).

The phunction PHP framework (http://sourceforge.net/projects/phunction/) uses the following function to generate valid version 4 UUIDs:



<?phpfunction GUID()
{
    if (
function_exists('com_create_guid') === true)
    {
        return
trim(com_create_guid(), '{}');
    }
    return
sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));

}
?>

The output generated by the sprintf() and mt_rand() calls is identical to com_create_guid() results.


Friday 28 November 2014

spl_autoload What is spl_autoload function in php

spl_autoload What is spl_autoload function in php

void spl_autoload ( string $class_name [, string $file_extensions = spl_autoload_extensions() ] )
Default implementation for __autoload()
This function is intended to be used as a default implementation for __autoload(). If nothing else is specified and spl_autoload_register() is called without any parameters then this function will be used for any later call to __autoload().
spl_autoload_register() allows you to register multiple functions (or static methods from your own Autoload class) that PHP will put into a stack/queue and call sequentially when a "new Class" is declared.

Following is the example
 
<?php
spl_autoload_register('myAutoloader');
function loadAutoload($className){
    $path = '/path/to/class';
    include $path.$className.'.php';
}
$obj = new Zend_Log();
?>

Thursday 27 November 2014

parse_ini_string - Parse a configuration string

array parse_ini_string ( string $ini [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL ]] )
Parse a configuration string
parse_ini_string returns the settings in string ini in an associative array.

Function parse_ini_file doesn't parse a remote ini file if allow_url_include is off. But if allow_url_fopen is on, you can use parse_ini_string to parse a remote ini file after read its contents.
/**
* Assume that; allow_url_include=0 and allow_url_fopen=1
* (default values in php.ini)
*/

$iniUrl = 'http://website.com/remote/config.ini';
/**
* Warning: parse_ini_file(): http:// wrapper is disabled in the server configuration by allow_url_include=0
*/
$config = parse_ini_file($iniUrl);

/**
* works fine
*/
$config = parse_ini_string(file_get_contents($iniUrl));

Wednesday 26 November 2014

stream_socket_client - Open Internet or Unix domain socket connection

resource stream_socket_client ( string $remote_socket [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") [, int $flags = STREAM_CLIENT_CONNECT [, resource $context ]]]]] )
Open Internet or Unix domain socket connection
Initiates a stream or datagram connection to the destination specified by remote_socket. The type of socket created is determined by the transport specified using standard URL formatting: transport://target. For Internet Domain sockets (AF_INET) such as T

<?php
$fp 
stream_socket_client("tcp://www.example.com:80"$errno$errstr30);
if (!
$fp) {
    echo 
"$errstr ($errno)<br />\n";
} else {
    
fwrite($fp"GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
    while (!
feof($fp)) {
        echo 
fgets($fp1024);
    }
    
fclose($fp);
}
?>


Tuesday 25 November 2014

imagecopyresampled-Copy and resize part of an image with resampling

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
Copy and resize part of an image with resampling
imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.

<?php// The file

$filename 'test.jpg';
$percent 0.5;// Content type
header('Content-Type: image/jpeg');// Get new dimensions
list($width$height) = getimagesize($filename);$new_width $width $percent;$new_height $height $percent;// Resample
$image_p imagecreatetruecolor($new_width$new_height);
$image imagecreatefromjpeg($filename);
imagecopyresampled($image_p$image0000$new_width$new_height$width$height);// Output
imagejpeg($image_pnull100);?>


Thursday 2 October 2014

SPL autoload extensions in PHP

spl autoload extensions in PHP

string spl_autoload_extensions ([ string $file_extensions ] )
Register and return default file extensions for spl_autoload
This function can modify and check the file extensions that the built in __autoload() fallback function spl_autoload() will be using.
<?php
    public function __construct() 
    {
        spl_autoload_register(array($this,'library'));
    }

    public function library($class)
    {
        if (is_string($class) && null === $class) {
            set_include_path(get_include_path().PATH_SEPARATOR.'app/lib/');
            spl_autoload_extension('.php');
            spl_autoload($class);
        }
        return false;
    }
?>

Parse a binary IPTC block into single tags

array iptcparse ( string $iptcblock )

Parse a binary IPTC block into single tags.

Parses an » IPTC block into its single tags.

You might have noticed that several metadata fields in Photoshop are not available via IPTC.



Someone have written a library "PHP JPEG Metadata Toolkit" which fixes this problem as it allows reading, writing and interpreting of virtually any type of metadata, including these missing fields.


Try it out:
http://www.ozhiker.com/electronics/pjmt/index.html



Function Return Value
It return an array using the tagmarker as an index and the value as the value and  returns FALSE on error or if no IPTC data was found.