Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

10 December 2015

Repeat a string in PHP with iteration counter - quick and easy function

When you're working through some complex program logic, low level code like manipulating strings is a distraction to write and worse, when you come back to it six months later, it makes your code look long and daunting to get back into. With this in mind, I always try to wrap low level code in re-usable functions.

Here's one such handy little function which works just like PHP's native str_repeat but it will include the iteration count in the string itself and do some cleaning at the end. This is perfect for generating numbered lists, sql queries and a bunch of other uses.

Continue Reading >

06 May 2010

PHP: Ultimate get/fetch RELATIVE PATH function between 2 folders/directories/files/pages or root dir

I wrote these functions today when everything I found online that attempted the same job was either too bulky, or too restrictive. It took me flippin ages - far longer than it should have! But it has huge functionality and will be a great addition to the arsenal. The main function relpath basically calculates the relative path from anywhere to anywhere on the same domain. Possible uses are listed below:
  1. Get relative path from the current page to the root directory of the local domain (rootpath)...
    echo relpath(); //outputs ../../ or something similar
  2. Get relative path from the current page to any other file/folder on the domain...
    echo relpath("/scripts/javascripts/jsmin/min.js"); //outputs ../../jsmin/min.js
  3. Get relative path from any file/folder on the domain to any other file/folder on the domain...
    echo relpath("/scripts/connect.php","/scripts/javascripts/jsmin"); //outputs ../../scripts.connect.php
Full functions with details and comments below.
Continue Reading >

02 May 2010

PHP: Quick & handy browser sniffer/checker function/class

Yes, you can check your visitor's browser using PHP! What's more, it never goes out of date! It automatically updates to recognise the lastest browsers. As much as I'd like to, I can't take credit for this one - it's the work of Jonathan Stoppani and makes browser sniffing with PHP an absolute breeze.

This class (named Browscap) is a great alternative to PHP's native get_broswer function which requires manual regular updating of a browser.ini definitions file. Instead, Browscap intermittently imports the latest browser definitions from browserproject.com  so that your scripts are always up to speed with the latest browser information.
Instructions and download below...

Continue Reading >

21 April 2010

PHP: Image resize function for calculating & constraining image proportions to fit in a box or space

I wrote this function on the back of my Actionscript version to calculate the output size of an image. If you only have a certain amount of space available in your page layout and you need to dynamically display an image that you don't know the size of, then this function is for you.

You simply specify how much space you have and the function will calculate the new size of the image proportionately and (optionally) return the html img tag for the browser. Sounds simple but it's incredibly useful and a lot easier than coding this each time you have need.

Continue Reading >

27 November 2009

PHP: How to Compare Directory Attributes & all files within

I wrote this function today to compare the contents of two folders or directories based on 3 comparison points:

  • Do files exist in both directories?
  • Were they both modified at the same time?
  • Are the contents identical?

This function returns a 3D array containing a list of all differences between the contents of the two directories...

Continue Reading >

24 November 2009

PHP: How to upload ZIP, check if file exists within it & extract to the server

I had to do this today for a job. It took far longer than it should have. The resources online are minimal and php.net does not mention any keywords like file_exists on manual for the function needed to do this. so...

Here is the best way to check if an uploaded zip archive contains a certain file and if so, extract the entire contents to a directory on your server:

Continue Reading >

11 November 2009

AS2: PHP style trim for Actionscript 2 - with 'any' character support

Theres a few of these functions around but none (that I could find) will allow you to trim non whitespace characters so I pulled this one together which lets you trim any character you want.

//USAGE:
trace(trim(";A test;;", ";"));    //outputs: "A test" 

function trim(str:String, char:String):String{
    if(char==undefined){char=" ";}
    code=char.charCodeAt(0)
    for(var i = 0; str.charCodeAt(i) ==code; i++);
    for(var j = str.length-1; str.charCodeAt(j) ==code; j--);
    return str.substring(i, j+1);
}

14 October 2009

mySQL to Flash import class - run queries from AS2 simply & automatically

mySQL2AS package with DataGrid...

This incredibly simple tool takes 5 minutes to set up and allows you to run mySQL queries and return results to flash using Actionscript 2 just as you can in PHP or any other language. Just 3 lines at the top of your flash document make mySQL queries a single function call away.  You can loop through the results, trace them all to the output panel and even display them in a datagrid (a flash table) on the stage.
Continue Reading >

04 October 2009

Save JPG /PNG image from AS2 project using AS3 & PHP

There's a tonne of Actionscript 2 image capture scripts out there but after an extensive search last month, the best ones I could find all ran into horrible memory problems and server timeouts when trying to capture large images. So I figured out a better way...
Continue Reading >

18 September 2009

PHP class to Import/Export mySQL to CSV or Excel

Import/Export mySQL to CSV class:
This class (not really a class but thought the keyword "class" might help people searching for stuff like this) contains a function which can be fed a mySQL query and a filename. The query is run and the results are saved to a CSV file. There is also a function to import the CSV file back into a database.

Continue Reading >
Cron Design Studio: Dublin based web design & software development