Archive for the ‘php’ Category

array_diff_key

Tuesday, June 2nd, 2009 by dreamluverz

(PHP 5 >= 5.1.0RC1)

array_diff_key – Computes the difference of arrays using keys for comparison

Description

array array_diff_key ( array array1, array array2 [, array ...] )
array_diff_key() returns an array containing all the values of array1 that have keys that are not present in any of the other arguments. Note that the associativity is preserved. This function is like array_diff() except the comparison is done on the keys instead of the values.

Example 1. array_diff_key() example

<?php
$array1
= array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);

var_dump(array_diff_key($array1, $array2));
?>

The above example will output:

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.

Note: Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]);.

source: http://ph.php.net/array_diff





string to constant

Friday, May 22nd, 2009 by dreamluverz

constant (PHP 4 >= 4.0.4, PHP 5) constant – Returns the value of a constant Description mixed constant ( string name )constant() will return the value of the constant indicated by name. constant() is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function. This function works also with class constants.





Example: define(‘MYCONSTANT’, ‘HELLO’);

echo constant(‘MYCONSTANT’);
HELLO // result

source:http://www.php.net/constant

php string functions

Friday, May 22nd, 2009 by dreamluverz

Name Description
ASCII() Return numeric value of left-most character
BIN() Return a string representation of the argument
BIT_LENGTH() Return length of argument in bits
CHAR_LENGTH() Return number of characters in argument
CHAR() Return the character for each integer passed
CHARACTER_LENGTH() A synonym for CHAR_LENGTH()
CONCAT_WS() Return concatenate with separator
CONCAT() Return concatenated string
ELT() Return string at index number
EXPORT_SET() Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string
FIELD() Return the index (position) of the first argument in the subsequent arguments
FIND_IN_SET() Return the index position of the first argument within the second argument
FORMAT() Return a number formatted to specified number of decimal places
HEX() Return a hexadecimal representation of a decimal or string value
INSERT() Insert a substring at the specified position up to the specified number of characters
INSTR() Return the index of the first occurrence of substring
LCASE() Synonym for LOWER()
LEFT() Return the leftmost number of characters as specified
LENGTH() Return the length of a string in bytes
LIKE Simple pattern matching
LOAD_FILE() Load the named file
LOCATE() Return the position of the first occurrence of substring
LOWER() Return the argument in lowercase
LPAD() Return the string argument, left-padded with the specified string
LTRIM() Remove leading spaces
MAKE_SET() Return a set of comma-separated strings that have the corresponding bit in bits set
MATCH Perform full-text search
MID() Return a substring starting from the specified position
NOT LIKE Negation of simple pattern matching
NOT REGEXP Negation of REGEXP
OCTET_LENGTH() A synonym for LENGTH()
ORD() Return character code for leftmost character of the argument
POSITION() A synonym for LOCATE()
QUOTE() Escape the argument for use in an SQL statement
REGEXP Pattern matching using regular expressions
REPEAT() Repeat a string the specified number of times
REPLACE() Replace occurrences of a specified string
REVERSE() Reverse the characters in a string
RIGHT() Return the specified rightmost number of characters
RLIKE Synonym for REGEXP
RPAD() Append string the specified number of times
RTRIM() Remove trailing spaces
SOUNDEX() Return a soundex string
SOUNDS LIKE(v4.1.0) Compare sounds
SPACE() Return a string of the specified number of spaces
STRCMP() Compare two strings
SUBSTR() Return the substring as specified
SUBSTRING_INDEX() Return a substring from a string before the specified number of occurrences of the delimiter
SUBSTRING() Return the substring as specified
TRIM() Remove leading and trailing spaces
UCASE() Synonym for UPPER()
UNHEX()(v4.1.2) Convert each pair of hexadecimal digits to a character
UPPER() Convert to uppercase

String-valued functions return NULL if the length of the result would be greater than the value of the max_allowed_packet system variable. See Section 7.5.3, “Tuning Server Parameters”.

source: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

preg_match case insensitive

Thursday, May 21st, 2009 by dreamluverz

preg_match case insensitive
<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

file_get_contents vs fread

Thursday, May 21st, 2009 by dreamluverz

The main difference between a complete file read using fread and
file_get_contents is that file_get_contents may use mmap (if your OS supports
it) to use file memory mapping, which can eliminate a little of the copying
and/or allocation of memory in getting it from the file to PHP’s memory space

Whether it makes any difference to your system depends on your system and you
should benchmark it in relation to the rest of your script before worrying too
much about it.

Always beware of premature optimisation – you probably have larger things to
worry about first – file_get_contents() may be a slightly faster way to read
the whole file into memory, but it’s possible that it may be more efficient to
read the file in chunks and process those smaller chunks – everything depends
on context.

For reference, here are some timings from a trivial pair of scripts against a
70kB file:

Rate fread file_get_contents
fread 1562/s — -9%
file_get_contents 1724/s 10% –
source: http://bytes.com/forum/thread560079.html

pdflib

Monday, April 27th, 2009 by dreamluverz
Package Information
Summary Creating PDF on the fly with the PDFlib library
Maintainers Rainer Schaaf <rjs@pdflib.com> (lead) [details]
Uwe Steinmann <uwe@steinmann.cx> (lead) [details]
License PHP
Description This extension wraps the PDFlib programming library
for processing PDF on the fly, created by Thomas Merz.

PDFlib is available for commercial licensing and under the PDFlib Lite License
(see http://www.pdflib.com/fileadmin/pdflib/pdf/license/PDFlib-Lite-license.pdf)

More info on how to use PDFlib with PHP can be found at

http://www.pdflib.com/developer-center/technical-documentation/php-howto

Homepage www.pdflib.com

YOu can get the pdflib here http://pecl.php.net/package/pdflib