Categories
PHP

Read a Word doc using COM

So I managed to write a script to read a Microsoft Word document using PHP CLI and the COM object pretty easily, but then battled to get it working in the browser. In your php.ini: ; allow Distributed-COM calls; http://php.net/com.allow-dcomcom.allow_dcom = true On your Windows PC: Run “dcomcnfg” (right-click Windows icon and select Run) to open […]

Categories
PHP

PHP Dynamic year selector

Another post from the archives. After 2009 rolled in and all our sites failed to follow suite because  “someone” hard-coded the years – but only up to 2008 – I quickly had to write something a bit more – well, smart. I needed an HTML drop-down box with a set of years starting from a […]

Categories
PHP

PHP CLI “Hello World”

I have only recently begun working with PHP CLI scripts. Executing PHP scripts from the command line is fairly easy and you require no extra knowledge to create a basic script. As you progress you will need to know some new methods of using PHP. Below is a PHP script called hello.php which is executed […]

Categories
PHP

PHP short tags

Another post from the archives: I am running a WAMP development environment at home and at work. I am always looking for new ways to fine-tune my set-up. Here is one… The short open tags option allow you to leave off the “php” in the opening tag of your code block. So, instead of: <?php […]

Categories
PHP

PHP / MySQL: storing database credentials in an .ini file

It is recommended that you do not include database connection details in your scripts, so where exactly do you store them? Luckily, PHP has the handy function parse_ini_file() specifically for this task. From php.net: parse_ini_file ( string $filename [, bool $process_sections = false [, int $scanner_mode = INI_SCANNER_NORMAL ]] ) The $process_sections parameter allows you to […]

Categories
PHP

Bar-code generator for PHP

Whilst fiddling around with QR Codes, I came across this Bar-code generator for PHP. Minimum requirements: PHP 5 GD2 Extension Here is my code for a basic implementation (adapted from the manual which has errors): <?php require_once(‘class/BCGFontFile.php’); require_once(‘class/BCGColor.php’); require_once(‘class/BCGDrawing.php’); require_once(‘class/BCGcode128.barcode.php’); // The arguments are R, G, and B for color. $colorBack = new BCGColor(255, 255, […]

Categories
PHP

E-mail address validation

Hoo boy we have to do this all the time and a quick Google tells me everyone has their favorite little tricks. Some even work. Here are the results of an exercise in e-mail validation using two different PHP options, namely: filter_var($email, FILTER_VALIDATE_EMAIL)); preg_match(‘/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i’, $email); See demo page here…

Categories
PHP

Upload PDF script

This was a quick hack to upload a PDF to Clearer Conscience. This script was a quick hack to allow a PDF to be uploaded to replace an outdated one on Clearer Conscience. As the PDF link on the site is hard-coded, the new PDF must have the same name as the old one. You […]

Categories
PHP

Return the file extension portion of a filename

This was another question in a skills evaluation test Write a regular expression for the following code that will trim off the extension from a filename: $ext = preg_replace(‘/xxxxx/’, ’’, $filename); My solution: $ext = preg_replace(‘/^.*\.([^.]+)$/D’, ‘$1’, $filename); I find regular expressions pretty tricky, so usually end up using an online tool like this: In […]

Categories
PHP

CSV to SQL converter script using PHP

This is a very old script I wrote to take CSV data (exported from Excel) and create INSERT statements for an existing MySQL database table. Lots of room for improvement, and certainly not safe for use in a production environment in this form – but as a tool it gets the job done. <html> <head> […]