Its My Space

Cooooooooooool

Archive for the ‘Functions’ Category

Date Time Functions in php

Posted by Ramkumar on February 13, 2009

checkdate() : Validates a Gregorian date

date_default_timezone_get() : Returns the default time zone

date_default_timezone_set() : Sets the default time zone

date_sunrise() : Returns the time of sunrise for a given day / location

date_sunset() : Returns the time of sunset for a given day / location

date() Formats a local time/date getdate() : Returns an array that contains date and time information for a Unix timestamp

gettimeofday() : Returns an array that contains current time information

gmdate() : Formats a GMT/UTC date/time

gmmktime() : Returns the Unix timestamp for a GMT date

gmstrftime() : Formats a GMT/UTC time/date according to locale settings

idate() : Formats a local time/date as integer

localtime() : Returns an array that contains the time components of a Unix timestamp

microtime() : Returns the microseconds for the current time

mktime() : Returns the Unix timestamp for a date

strftime() : Formats a local time/date according to locale settings

strptime() : Parses a time/date generated with strftime()

strtotime() : Parses an English textual date or time into a Unix timestamp

time() : Returns the current time as a Unix timestamp

Posted in Functions, PHP | Leave a Comment »

PHP MySQL Functions

Posted by Ramkumar on February 13, 2009

mysql_affected_rows() : Returns the number of affected rows in the previous MySQL operation

mysql_change_user() : Deprecated. Changes the user of the current MySQL connection

mysql_client_encoding() : Returns the name of the character set for the current connection

mysql_close() : Closes a non-persistent MySQL connection

mysql_connect() : Opens a non-persistent MySQL connection

mysql_create_db() : Deprecated. Creates a new MySQL database. Use mysql_query() instead

mysql_data_seek() : Moves the record pointer

mysql_db_name() : Returns a database name from a call to mysql_list_dbs()

mysql_db_query() : Deprecated. Sends a MySQL query. Use mysql_select_db() and mysql_query() instead

mysql_drop_db() : Deprecated. Deletes a MySQL database. Use mysql_query() instead

mysql_errno() : Returns the error number of the last MySQL operation

mysql_error() : Returns the error description of the last MySQL operation

mysql_escape_string() : Deprecated. Escapes a string for use in a mysql_query. Use mysql_real_escape_string() instead

mysql_fetch_array() : Returns a row from a recordset as an associative array and/or a numeric array

mysql_fetch_assoc() : Returns a row from a recordset as an associative array

mysql_fetch_field() : Returns column info from a recordset as an object

mysql_fetch_lengths() : Returns the length of the contents of each field in a result row

mysql_fetch_object() : Returns a row from a recordset as an object

mysql_fetch_row() : Returns a row from a recordset as a numeric array

mysql_field_flags() : Returns the flags associated with a field in a recordset

mysql_field_len() : Returns the maximum length of a field in a recordset

mysql_field_name() : Returns the name of a field in a recordset

mysql_field_seek() : Moves the result pointer to a specified field

mysql_field_table() : Returns the name of the table the specified field is in

mysql_field_type() : Returns the type of a field in a recordset

mysql_free_result() : Free result memory

mysql_get_client_info() : Returns MySQL client info

mysql_get_host_info() : Returns MySQL host info

mysql_get_proto_info() : Returns MySQL protocol info

mysql_get_server_info() : Returns MySQL server info

mysql_info() : Returns information about the last query

mysql_insert_id() : Returns the AUTO_INCREMENT ID generated from the previous INSERT operation

mysql_list_dbs() : Lists available databases on a MySQL server

mysql_list_fields() : Deprecated. Lists MySQL table fields. Use mysql_query() instead

mysql_list_processes() : Lists MySQL processes

mysql_list_tables() : Deprecated. Lists tables in a MySQL database. Use mysql_query() instead

mysql_num_fields() : Returns the number of fields in a recordset

mysql_num_rows() : Returns the number of rows in a recordset

mysql_pconnect() : Opens a persistent MySQL connection

mysql_ping() : Pings a server connection or reconnects if there is no connection

mysql_query() : Executes a query on a MySQL database

mysql_real_escape_string() : Escapes a string for use in SQL statements

mysql_result() : Returns the value of a field in a recordset

mysql_select_db() : Sets the active MySQL database

mysql_stat() : Returns the current system status of the MySQL server

mysql_tablename() : Deprecated. Returns the table name of field. Use mysql_query() instead

mysql_thread_id() : Returns the current thread ID

mysql_unbuffered_query() : Executes a query on a MySQL database (without fetching / buffering the result

Posted in Functions, PHP | 1 Comment »

Zip File Functions in php

Posted by Ramkumar on February 13, 2009

zip_close() :  Closes a ZIP file

zip_entry_close() :Closes an entry in the ZIP file

zip_entry_compressedsize() : Returns the compressed size of an entry in the ZIP file

zip_entry_compressionmethod() : Returns the compression method of an entry in the ZIP file

zip_entry_filesize() : Returns the actual file size of an entry in the ZIP file

zip_entry_name() : Returns the name of an entry in the ZIP file

zip_entry_open() : Opens an entry in the ZIP file for reading

zip_entry_read() : Reads from an open entry in the ZIP file

zip_open() : Opens a ZIP file

zip_read() :  Reads the next entry in a ZIP file

Posted in Functions, PHP | Leave a Comment »

String Function in php

Posted by Ramkumar on February 13, 2009

addcslashes() : Returns a string with backslashes in front of the specified characters

addslashes() : Returns a string with backslashes in front of predefined characters

bin2hex() : Converts a string of ASCII characters to hexadecimal values

chop() : Alias of rtrim()

chr() : Returns a character from a specified ASCII value

chunk_split() : Splits a string into a series of smaller parts

convert_cyr_string() : Converts a string from one Cyrillic character-set to another

convert_uudecode() : Decodes a uuencoded string

convert_uuencode() : Encodes a string using the uuencode algorithm

count_chars() : Returns how many times an ASCII character occurs within a string and returns the information

crc32() : Calculates a 32-bit CRC for a string

crypt() : One-way string encryption (hashing)

echo() : Outputs strings

explode() : Breaks a string into an array

fprintf() : Writes a formatted string to a specified output stream

get_html_translation_table() : Returns the translation table used by htmlspecialchars() and htmlentities()

hebrev() : Converts Hebrew text to visual text

hebrevc() : Converts Hebrew text to visual text and new lines (\n) into <br />

html_entity_decode() : Converts HTML entities to characters

htmlentities() : Converts characters to HTML entities

htmlspecialchars_decode() : Converts some predefined HTML entities to characters

htmlspecialchars() : Converts some predefined characters to HTML entities

implode() : Returns a string from the elements of an array

join() : Alias of implode()

levenshtein() : Returns the Levenshtein distance between two strings

localeconv() :Returns locale numeric and monetary formatting information

ltrim() :  Strips whitespace from the left side of a string

md5() : Calculates the MD5 hash of a string

md5_file() : Calculates the MD5 hash of a file

metaphone() : Calculates the metaphone key of a string

money_format() : Returns a string formatted as a currency string

nl_langinfo() : Returns specific local information

nl2br() : Inserts HTML line breaks in front of each newline in a string

number_format() : Formats a number with grouped thousands

ord() : Returns the ASCII value of the first character of a string

parse_str() : Parses a query string into variables

print() : Outputs a string

printf() : Outputs a formatted string

quoted_printable_decode() : Decodes a quoted-printable string

quotemeta() : Quotes meta characters

rtrim() :Strips whitespace from the right side of a string

setlocale() : Sets locale information

sha1() : Calculates the SHA-1 hash of a string

sha1_file() : Calculates the SHA-1 hash of a file

similar_text() : Calculates the similarity between two strings

soundex() : Calculates the soundex key of a string

sprintf() : Writes a formatted string to a variable

sscanf() : Parses input from a string according to a format

str_ireplace() : Replaces some characters in a string (case-insensitive)

str_pad() : Pads a string to a new length

str_repeat() : Repeats a string a specified number of times

str_replace() : Replaces some characters in a string (case-sensitive)

str_rot13() : Performs the ROT13 encoding on a string

str_shuffle() : Randomly shuffles all characters in a string

str_split() : Splits a string into an array

str_word_count() : Count the number of words in a string

strcasecmp() : Compares two strings (case-insensitive)

strchr() : Finds the first occurrence of a string inside another string (alias of strstr())

strcmp() : Compares two strings (case-sensitive)

strcoll() : Locale based string comparison

strcspn() : Returns the number of characters found in a string before any part of some specified characters are found

strip_tags() : Strips HTML and PHP tags from a string

stripcslashes() : Unquotes a string quoted with addcslashes()

stripslashes() : Unquotes a string quoted with addslashes()

stripos() : Returns the position of the first occurrence of a string inside another string (case-insensitive)

stristr() : Finds the first occurrence of a string inside another string (case-insensitive)

strlen() : Returns the length of a string

strnatcasecmp() : Compares two strings using a “natural order” algorithm (case-insensitive)

strnatcmp() : Compares two strings using a “natural order” algorithm (case-sensitive)

strncasecmp() : String comparison of the first n characters (case-insensitive)

strncmp() :String comparison of the first n characters (case-sensitive)

strpbrk() : Searches a string for any of a set of characters

strpos() : Returns the position of the first occurrence of a string inside another string (case-sensitive)

strrchr() : Finds the last occurrence of a string inside another string

strrev() : Reverses a string

strripos() : Finds the position of the last occurrence of a string inside another string (case-insensitive)

strrpos() : Finds the position of the last occurrence of a string inside another string (case-sensitive)

strspn() : Returns the number of characters found in a string that contains only characters from a specified charlist

strstr() : Finds the first occurrence of a string inside another string (case-sensitive)

strtok() : Splits a string into smaller strings

strtolower() : Converts a string to lowercase letters

strtoupper() : Converts a string to uppercase letters

strtr() : Translates certain characters in a string

substr() : Returns a part of a string

substr_compare() : Compares two strings from a specified start position (binary safe and optionally case-sensitive)

substr_count() : Counts the number of times a substring occurs in a string

substr_replace() : Replaces a part of a string with another string

trim() : Strips whitespace from both sides of a string

ucfirst() :Converts the first character of a string to uppercase

ucwords() : Converts the first character of each word in a string to uppercase

vfprintf() : Writes a formatted string to a specified output stream

vprintf() : Outputs a formatted string

vsprintf() : Writes a formatted string to a variable

wordwrap() :Wraps a string to a given number of characters

Posted in Functions, PHP | Leave a Comment »

Preg in PHP

Posted by Ramkumar on February 12, 2009

Preg_Grep PHP Function

The PHP function, preg_grep, is used to search an array for specific patterns and then return a new array based on that filtering. There are two ways to return the results. You can return them as is, or you can invert them (instead of only returning what matches, it would only return what does not match.) It is phrased as: preg_grep ( search_pattern, $your_array, optional_inverse ) The search_pattern needs to be a regular expression. If you are unfamiliar with them this article gives you an overview of the syntax.

<?
$data = array(0, 1, 2, ‘three’, 4, 5, ’six’, 7, 8, ‘nine’, 10);
$mod1 = preg_grep(“/4|5|6/”, $data);
$mod2 = preg_grep(“/[0-9]/”, $data, PREG_GREP_INVERT);
print_r($mod1);
echo “<br>”;
print_r($mod2);
?>

This code would result in the following data:
Array ( [4] => 4 [5] => 5 )
Array ( [3] => three [6] => six [9] => nine )

First we assign our $data variable. This is a list of numbers, some in alpha form, others in numeric. The first thing we run is called $mod1. Here we are searching for anything that contains 4, 5, or 6. When our result is printed below we only get 4 and 5, because 6 was written as ’six’ so it did not match our search.

Next we run $mod2, which is searching for anything that contains a numeric character. But this time we include PREG_GREP_INVERT. This will invert our data, so instead of outputting numbers, it outputs all of our entries that where not numeric (three, six and nine).

Preg_Match PHP Function

The Preg_Match PHP function is used to search a string, and return a 1 or 0. If the search was successful a 1 will be returned, and if it was not found a 0 will be returned. Although other variables can be added, it is most simply phrased as: preg_match(search_pattern, your_string). The search_pattern needs to be a regular expression. If you are unfamiliar with them this article gives you an overview of the syntax.

<?
$data = “I had a box of cerial for breakfast today, and then I drank some juice.”;
if (preg_match(“/juice/”, $data))
{
echo “You had juice.<br>”;
}
else
{
echo “You had did not have juice.<br>”;
}
if (preg_match(“/eggs/”, $data))
{
echo “You had eggs.<br>”;
}
else
{
echo “You had did not have eggs.<br>”;
}
?>

The code above uses preg_match to check for a key word (first juice then egg) and replies based on whether it is true (1) or false (0). Because it returns these two values it is most often used in a conditional statement.

Preg_Match_All PHP Function
Preg_Match_All is used to search a string for specific patterns and stores the results in an array. Unlike preg_match which stops searching after it finds a match, preg_match_all searches the entire string and records all matches. It is phrased as: preg_match_all (pattern, string, $array, optional_ordering, optional_offset)

<? $data = “The party will start at 10:30 pm and run untill 12:30 am”;
preg_match_all(‘/(\d+:\d+)\s*(am|pm)/’, $data, $match, PREG_PATTERN_ORDER);
echo “Full: <br>”;
print_r($match[0]);
echo “<p>Raw: <br>”;
print_r($match[1]);
echo “<p>Tags: <br>”;
print_r($match[2]);
?>

In our first example we use PREG_PATTERN_ORDER. We are searching for 2 things; one is the time, the other is it’s am/pm tag. Our results are outputted to $match, as an array where $match[0] contains all matches, $match[1] contains all data matching our first sub-serach (the time) and $match[2] contains all data matching our second sub-search (am/pm).

<? $data = “The party will start at 10:30 pm and run untill 12:30 am”;
preg_match_all(‘/(\d+:\d+)\s*(am|pm)/’, $data, $match, PREG_SET_ORDER);
echo “First: <br>”;
echo $match[0][0] . ” , ” . $match[0][1] . ” , “. $match [0][2] .”<br>”;
echo “Second: <br>”;
echo $match[1][0] . ” , ” . $match[1][1] . ” , “. $match [1][2] .”<br>”;
?>

In our second example we use PREG_SET_ORDER. This puts each full result into an array. The first result is $match[0], with $match[0][0] being the full match, $match[0][1] being the first sub-match and $match[0][2] being the second sub-match.

Preg_Replace PHP Function

The preg_replace function is used to do a find-and-replace on a string or an array. We can give it one thing to find and replace (for example it seeks out the word ‘him’ and changes it to ‘her’) or we can give it a full list of things (an array) to search for, each with a corresponding replacement. It is phrased as preg_replace ( search_for, replace_with, your_data , optional_limit, optional_count ) The limit will default to -1 which is no limit. Remember your_data can be a string or an array.

<?
$data = “The cat likes to sit on the fence. He also likes to climb the tree.”;

$find =”/the/”;
$replace =”a”;

//1. replace single word
Echo “$data <br>”;
Echo preg_replace ($find, $replace, $data);

//create arrays
$find2 = array (‘/the/’, ‘/cat/’);
$replace2 = array (‘a’, ‘dog’);

//2. replace with array values
Echo preg_replace ($find2, $replace2, $data);

//3. Replace just once
Echo preg_replace ($find2, $replace2, $data, 1);

//4. Keep a count of replacements
$count = 0;
Echo preg_replace ($find2, $replace2, $data, -1, $count);
Echo “<br>You have made $count replacements”;

?>

In our first example we simply replace ‘the’ with ‘a’. As you can see these are cAse seNsiTIvE. Then we set up an array, so in our second example we are replacing both the words ‘the’ and ‘cat’. In our third example, we set the limit to 1, so each word is only replaced one time. Finally in our 4th example, we keep count of how many replacements we have made.

Preg_Split PHP Function

The function Preg_Spilit is used to take a string, and put it into an array. The string is broken up into different values in the array based upon your input. It is phrased as preg_split ( split_pattern, your_data, optional_limit, optional_flags )

<?php
$str = ‘I like goats. You like cats. He likes dogs.’;
$chars = preg_split(‘//’, $str);
print_r($chars);
echo “<p>”;
$words= preg_split(‘/ /’, $str);
print_r($words);
echo “<p>”;
$sentances= preg_split(‘/\./’, $str, -1 , PREG_SPLIT_NO_EMPTY);
print_r($sentances);
?>

In the code above we preform three splits. In our first, we split the data by each character. In the second, we split it with a blank space, thus giving each word (and not each letter) an array entry. And in our third example we use a ‘.’ period to split the data, therefor giving each sentence it’s own array entry.

Because in our last example we use a ‘.’ period to split, a new entry is started after our final period, so we add the flag PREG_SPLIT_NO_EMPTY so that no empty results are returned. Other available flags are PREG_SPLIT_DELIM_CAPTURE which also captures the character you are splitting by (our “.” for example) and PREG_SPLIT_OFFSET_CAPTURE which captures the offset in characters where the split has occurred.

Posted in Functions, PHP | Tagged: , | Leave a Comment »