Tag Archives: PHP

Url safe base64 encoding and decoding in PHP

Whenever passing base64 encoded strings in url should always urlencode or do the following which replaces characters that should not be in a query string value with safe characters so query string will not be malformed. When you use the encode method must also use the decode method to convert those characters back to the […]

Custom File Upload Error Messages

You can set custom error messages using the built-in php upload error constants. http://www.php.net/manual/en/features.file-upload.errors.php $upload_errors = array ( UPLOAD_ERR_OK => ‘No errors.’, UPLOAD_ERR_INI_SIZE => ‘Larger then upload_max_filesize.’, UPLOAD_ERR_FORM_SIZE => ‘File size is to large!’, UPLOAD_ERR_PARTIAL => ‘Resume partially uploaded!’, UPLOAD_ERR_NO_FILE => ‘You must upload a resume!’, UPLOAD_ERR_NO_TMP_DIR => ‘Temporary directory does not exist!’, UPLOAD_ERR_CANT_WRITE => […]

Using Stored Procedures in Msyql with PHP

This will take an input and output a single value. Must define input and output data types. $db = mysqli_connect(“127.0.0.1″, “dash_rw”, “password”, “database”); $db->query(“DROP PROCEDURE IF EXISTS p”); // take input and output single value. Output does not have to match table field length. $db->query(“CREATE PROCEDURE p(IN id_val INT, OUT pname varchar(100)) BEGIN SELECT name […]

Delete multiple files using unlink and array_map()

This will delete all files in a directory matching specific pattern. Applies unlink() to each file that meet glob pattern. array_map(‘unlink’, glob(“some/dir/*.txt”));

Accessing POP Emails in PHP using imap library

With the use of the IMAP library you will be able to access pop3 email inboxes. The following will access a pop3 inbox, get count of emails and print out basic details about the emails. $user = ‘someuser@somedomain.com’; $pass = ‘somepassword’; $host = ’255.255.0.1′; // Connect to the pop3 email inbox belonging to $user // […]

Processing files from zip and tar.bz2 files using Phar extension

As of php 5.3 the phar extension is built-in. For earlier versions can install using pear. The following will open a .zip or .tar.bz2 file and loop through the files in specified archive directory. The phar path can be treated like a disk directory. $uploaded_filename = $_FILES[ "userfile" ][ "tmp_name" ]; $dest_path = ‘/path/to/file/’ . […]

Using Correct Callback Method Scope Within a PHP Class

If your calling method in a class has a callback function as a parameter such as usort() or array_walk() you need to call the callback function in the correct scope or php will not find it. If your using array_walk() within a class method then you need to define the callback this way. class SomeClass […]

HTTP Requests Using Curl and Decoding JSON Responses in PHP

You can do http requests using the Curl library and decode the JSON result with json_decode() function. PHP 5.2 and above comes with the JSON library functions. The following example will send a request to brightcove’s api using curl and extract some video thumbnail image links. The result will be a list of links in […]

Extracting n Number Of Words From String in PHP

This function will extract a number of words from a string. Uses a regular expression search to get the words. function get_words($str,$count) { return implode(" ", array_slice(preg_split("/\s+/", $str), 0, $count)); } $string = "In Scott Pilgrim vs. the World, the titular character is definitely experiencing girl problems."; echo get_words($string,10).’…’; This example will print out the […]

Find Matches Using PHP preg_match() Regular Expression Function

This pattern will extract the url from a link tag For some reason in PHP you cannot use the common / regular expression delimiter in the pattern. I use either single quote or # as the delimiter. The whole pattern is then surrounded by double quotes. $link = ‘<a href="http://some.com">some site</a>’; //(.*?) returns everything within […]