Archive for PHP Tips

PHP tip: Best way to get file extension

// July 25th, 2010 // 2 Comments » // PHP, PHP Tips

I tried 100 ways to get the file extension from a complete file path including directories and file name with extension. This one works perfectly. This uses php function pathinfo to get the directory name, filename and its extension for you which is very handy.

<?php
$fileinfo = pathinfo(“/path/to/your/file.php”);
echo $fileinfo['dirname']; // Get directory
echo $fileinfo['basename']; // Get file basename
echo $fileinfo['extension']; // Get file extension
echo $fileinfo['filename']; // Get file name
?>

Testing zend optimizer encoded PHP applications

// July 4th, 2010 // No Comments » // PHP, PHP Tips

I am working on an expressionengine application these days. As I am using the beta version of expression engine until they release the final version, so the source files are encoded with the zend optimizer. Zend optimizer has a limitation that it can’t work with version 5.3 of PHP so far.

I had the latest XAMPP installed on my Windows VISTA machine for a year now but I found out that Zend optimiser isn’t compatible with version of PHP it provided i.e PHP 5.3, so I downloaded the version 1.6.8 of XAMPP from http://sourceforge.net/projects/xampp/files/ which has the older version of PHP i.e 5.2 which is compatible with Zend Optimizer.

Now here is the thing, I downloaded the Zend Optimizer from this link and installed it.  In its installation steps it asks you for two locations which you have to tell it correctly or else it will not work properly.

  1. The path to php.ini
  2. The path to Apache websrver’s root.

Based on the installation directory of your XAMPP, you can select appropriate directories in these steps. I chose default C:\xampp while installing XAMPP,  so I selected C:\xampp\apache\bin and C:\xampp\apache for the Steps 1 and 2 respectively.

And before that, using XAMPP control panel, I had stopped the Apache so when the configuration changes take place, the next time apache runs, it loads this new php.ini file which is changed by the zend optimizer installation.

Next thing you want to do is, check the php info using phpinfo.php file using the follwing url.

http://localhost/xampp/phpinfo.php

It will open up a page with a bunch of information. Look for the text  Loaded Configuration File and open the same php file using windows explorer.

For me it was

Loaded Configuration File C:\xampp\apache\bin\php.ini

So I opened the php.ini folder and searched for the line where Zend Optimiser properties were mentioned and

[Zend]
zend_extension_ts="C:\Program Files\Zend\ZendOptimizer-3.3.0\lib\ZendExtensionManager.dll"
zend_extension_manager.optimizer_ts="C:\Program Files\Zend\ZendOptimizer-3.3.0\lib\Optimizer-3.3.0"
zend_optimizer.enable_loader = 0
zend_optimizer.optimization_level=15
;zend_optimizer.license_path =
; Local Variables:
; tab-width: 4
; End:

[Zend]zend_extension_ts="C:\Program Files\Zend\ZendOptimizer-3.3.0\lib\ZendExtensionManager.dll"zend_extension_manager.optimizer_ts="C:\Program Files\Zend\ZendOptimizer-3.3.0\lib\Optimizer-3.3.0"zend_optimizer.enable_loader = 1zend_optimizer.optimization_level=15;zend_optimizer.license_path =; Local Variables:; tab-width: 4; End:

In the above code I changed


zend_optimizer.enable_loader = 0

to


zend_optimizer.enable_loader = 1

So its loaded the next time I start the apache.

PHP: Text cropping with defining the number of characters, words or sentences

// March 18th, 2009 // 2 Comments » // PHP, PHP Tips, Web Development

I have recently been working on PHP RSS reader script that was supposed to do three different things with RSS description text. To make a teaser RSS text, I was asked to develop the functionality in a such a way that text can be cropped in these three ways.

  • with defining the number of words.
  • with defining the numbers of characters.
  • with defining the number of sentences.

Here is a set of functions that helps you crop a text in three different ways. The usage of the function is also mentioned at the end f the script. (more…)

How to make sure your PHP RSS feed reader doesn’t mess up

// March 18th, 2009 // No Comments » // PHP, PHP Tips, Web Development

I was recently working on a custom RSS feed reader where the prior most concern of the client was being able to crop the text with respect to the

  1. Number of words.
  2. Numbers of characters.
  3. Number of sentences. (more…)

PHP: How to rewmove last comma from a string

// November 25th, 2008 // No Comments » // PHP Tips

Hello readers

A few minutes ago, I had a situation where

For example you have a string which is like “cat,cow,hamster,owl,”

and you want to remove the last comma from the string

then you can use this code

<?php
$string="cat,cow,hamster,owl,";
echo $string."<br/>";
$string= substr($string, 0, strlen($string)-1);
echo $string;
?>

This comes really handy in many situations.

Ajax loading button/ bar/ icon gif animation maker/generator with configurable background and foreground color

// December 10th, 2007 // 2 Comments » // AJAX, General, Open Source, Open Source Icons, PHP, PHP Tips, Web Development, Web Development Software

You are a web surfer right? Thats how you stumbled upon my site. You can rate this post by clicking on these rating stars with this post and yeah you see that cool when you rate a post here. Its an Ajax script working on the background showing you the nice status bar message with this icon. Its interactive and a good usable thing for your own Ajax script. Wanna make one like that for your ajax stuff? Yeah you can do so on the web!

Ajax loading gif generator Create easily your own ajax loader icon

Keep on reading to find the link to the amazing site! (more…)

PHP: for loop or foreach loop with mutiple arrays. Multiple arrays in a single foreach loop

// December 6th, 2007 // No Comments » // Open Source, PHP, PHP Tips, Web Development

Here is an example for toy to understand the concept of multiple arrays in a single loop in PHP. My friend and coworker Mohammad Khan (yeah he is from Peshawar and sits beside me in the office where we develop web applications) was working on a such a situation for an airline scheduling system for Intercontinental Airways with HTML form submission in PHP where he has multiple arrays passed as POST variables. So I did a quick search on internet and found solutions spread across the forums. I decided to do the quick and dirty mash up of all the stuff with proper editing to get you guys going on the same path along! (more…)