Category Archives: PHP

Handlebars helpers



In an application we are using Handlebars templating. However, we needed many custom helpers to do what we wanted to achieve. Here are some of those helpers. It may help someone who is looking for achieving similar things. Examples are included.

Adding/Subtracting

Character limit with trailing characters (optional).

Get free SMS alert on your server down (or something else)!



Well, first thing first. This blog may be little bit immoral even if it is not illegal by the provider terms of service. So, if you read this or later sentences, the responsibilities are on you.

—————-

I am going to share an idea that I’ve personally used a bit ago (2009) in my previous office to monitor the server and get notified when any server/service is down. I wrote a small PHP application that would do the tasks. Though I planned that time to blog about it, apparently, I forgot about it. The steps of the idea is:

  • A php chunk checks server status
  • If it finds something unexpected (or may be expected; depends on your logic :) ) it creates an event in your google calendar which is due just 1-3 minutes from now
  • If your SMS alert is setup in Google Calender, Google will email you about the event.

Here I will not show how to write a PHP script to monitor server. I just told it as I applied this idea for same purpose. I guess you will get lots if you google it. However, the initial version that I developed just checked site’s homepage (using CURL) and if they were returning 200 response within a sensible time limit. If the response was not 200, it would create the event for me.

How to create Event?

It’s very simple. First of all download the following classes from phpclasses.org

Put them in your project path. Create a php file. Lets assume it is createevent.php. Now include the wrapper file in createevent.php.

Continue reading

Line wrapping is finally available in Netbeans 7.0

Well, if you ask me why I left Netbeans and started using phpStorm; I will say first: Speed, second: Line wrapping along with few other reasons. I’ve used Netbeans for quite sometime but I was very irritated as it did not have good line wrapping support. Some users may wonder why I am saying ‘good’ as it did not have any line wrapping at all.

The fact is that, line wrapping was implemented in earlier versions too but was not enabled by default as it was not stable.  It can be enabled by editing a configuration file but it was causing lots of troubles while coding. However, as the latest version, v7.0, is released on 18th April 2011, line wrapping is now available without any such hacking and so far it is good to me.

How to enable Line Wrapping?

Go to Netbeans  -> Preferences (or press CMD + ,) -> Editor -> Formatting

You will notice a drop down labeled as ‘Line Wrap’. Choose ‘After Words’ or ‘Anywhere’ as per your wish and then hit OK.

That’s it. Simply open a file (opened files need re-opening) and you will not see horizontal scroll-bars :) .

Netbeans Linewrapping

Netbeans Linewrapping

Extending or replacing core classes in CodeIgniter 2x

It has been quite some time that CodeIgniter 2x has been released after a quite long waiting. There are several improvements in this latest release and upgrading from the 1x is quite easy. However, they have changed some file/folder organization in this latest release. I found these changes as good as it provides better application wise personalization. CodeIgniter’s previous file/folder structures were not (at least, by default) very good (without some/little modification) for hosting multiple application based on single core installation. However, in this version they organized it better.

On of the major change was made in this version in replacing and/or extending core classes. There was no note in their upgrade guide (but explains in their relevant doc page, which I found later). So I had to go through little trouble with it until Arafat Rahman helped me out!

However, the process is simple. In version 2x, they have introduced a new directory called ‘core’ (along with few others). All core classes that needs to be replaced or extended should be in this directory. I liked it very much, as I hated to put core classes in library directory in older versions.

Replacing a core class
Let’s say we want to replace a core class by our own version. Also let’s assume we want to replace the model class (actual name is CI_Model). Now follow these steps:

  • Create a directory named ‘core’ in your application directory, if it does not exist. If your application directory is default one, it would be ‘application/core’
  • Create a new file named Model.php.
  • Put the following line in the newly created empty file:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class CI_Model
{
 function __construct()
  {
  }
}

Now when you create model classes in current application extend the CI_Model as you were doing earlier, this class will be extended rather than the CI_Model class in the system/core/Model.php. However, when you do, think if you have enough reasons to replace the core class as you can do most, if not all, of the tasks by simply extending it.

Extending the core class

Rather than replacing, I find it much better to extend the core class and use that class in my application. The steps of extending the core class are also simple. Continue reading

Pun Warning – Warning system for PunBB

Quite long ago, I have developed this extension to warn users due to violating rules in Projanmo Forum. Many asked me for releasing it but I could not release it for various reasons mainly as I could not test in Non-Bangla forums.

Today I decided to release it and so I’ve downloaded punbb and installed and then tested it. Seems it is working fine. So, I am now releasing it.

1. Download and install it as you do other extensions. You will also need to install jQuery extension before installing it. Continue reading

My experience with phpXperts seminar 2010

phpXperts Seminar 2010, is my third program with phpXperts. Other than phpXperts programs, I have also joined in other programs like Facebook Developer Garage, SQABD Lightning Talks etc. However, this one is exceptional to me as I was one of the speakers in this session.

My initial gratitude goes to Hasin Hayder, who tried hard for first speakers like me. He even advised me about my topic “HTML5 – Web is getting sexy”! Continue reading

Searching and retrieving yahoo answers in PHP

I am working on an WordPress autobloggin plugin where I had to search yahoo answers site for any keyword and post the answers in WordPress. After checking their site, I see it is very easy now as they provide API for the questions and answers. I do not know when they introduced API for Answers but I do remember during my last check (quite long time ago), they did not have any API and scrapping was the only way.

Their API for searching and getting answers is very simple and easiest! However, for the better handling of my project, I have mad a class to find questions by any given keyword and then retrieve the answers of any specific question by question ID.

How to use?
Continue reading

Case inSensitive in_array()

You might already know that PHP’s in_array() function matches the string needle case sensitive way. However, today I was needed something that matches in case insensitive way. Before doing it myself, I googled it and I found a ready made solution. It is quite simple. Here it is:

function in_arrayi($needle, $haystack)
{
    foreach ($haystack as $value)
    {
        if (strtolower($value) == strtolower($needle))
        return true;
    }
    return false;
}

Please note that this case-insensitive version is, at least, 5 times slower than in_array().

Using prosperent.com API with PHP!


Currently I am working with Prosperent.com’s affiliate network and making an auto blog plugin for wordpress. The plugin is intended to fetch products from prosperent and create wordpress posts. You will earn commission from the sales through your affiliate link! The affiliate links will also be given by prosperent. So, you don’t need to think about it. You just need to requests their API with the API Key and they will mask the product URL with your affiliate ID so the impressions and clicks are accounted under your affiliate account.

Prosperent already provides a simple and beautiful PHP class that makes enjoyable while working with their API. You just need to write a few lines to get products from them.

Continue reading

Show RSS Feed within a WordPress Post – In Post RSS Feed plugin

This idea, showing a relevant RSS Feed at the end of the post, came to my mind day before yesterday. That time, I had to install the wp-exec plugin that can handle PHP codes inside the post. Using that, I have included an RSS Feed at the end of the post to show live contents from external site.

However, that is not suitable for all. All wordpress users don’t know PHP. Moreover, it is not simply process. if you make a syntax error, you take a lots of trouble. So, I thought, I will make it simple. And yes, the initial version is done. I have plan to release an low-cost premium version with many customizable features that I have in my mind.

How it works?

After you install the plugin, you configure it from WP-Admin -> Settings -> In Post RSS link and set the number of items to show and show/hide Feed Title that is given by you as you see in the screenshot below:

Continue reading