Rendering will_paginate links nicely with Twitter Bootstrap

When I was working with Moviemates (my one dream pet project), I had to use will_paginate gem for pagination of the movie listings. As the UI are based on Twitter Bootstrap, the will_paginate links (generated by its view helpers) were looking very ugly. Please check the following screenshot to see how bad it was looking!

I had to google a while for a decent look of these links. I’ve found a very effective but easy fix written by  oparrish and shared here. For your conveniences, the gist is attached here:

Continue reading →

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 →

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 →

Articlesnatch.com scraping tutorial, getting full article

In my last three tutorials I have discussed how to scrap contents from www.articlebase.com. In this part, I will show how to scrap contents from www.articlesnatch.com. However, unlike the previous tutorials, I will not use DOMDocument in this part. I will not use regular expressions either.

I will show how to get full article. I won’t show how to get articles/links under any category as articlesnatch.com offers feed for each category. So it is easy to get article summary and links of any category. As the feed does not include full text, I will just show how to get it.

Getting Article Body

$html = file_get_contents($link);

We need the contents that is within the div with a class named “KonaBody”. That mean, our target contents are within:

<div class="KonaBody">

......

......

</div>

So, we may remove anything before this div.

$desc = strstr($html,'<div class="KonaBody">');

Continue reading →

Smarter DOMDocument for PHP

PHP 5 includes a great built in class DOMDocument to DOM parsing of HTML/XML document. The class includes a number of methods to easily traversing a DOM.

However, it has a few shortfalls like it fails to handle encoding correctly and includes some tags which may often seem irritating for developers.

Artem Russakovskii has made an extension (named SmartDOMDocument) of this class to eliminate such shortcomings. His class inherits the built in DOMDocument and includes a few extra methods that may make developers life peaceful.

Extra Features of SmartDOMDocument


saveHTMLExact()

DOMDocument has an extremely badly designed “feature” where if the HTML code you are loading does not contain <html> and <body> tags, it adds them automatically (yup, there are no flags to turn this behavior off).

Thus, when you call $doc->saveHTML(), your newly saved content now has <html><body> and DOCTYPE in it. Not very handy when trying to work with code fragments (XML has a similar problem).

SmartDOMDocument contains a new function called saveHTMLExact() which does exactly what you would want – it saves HTML without adding that extra garbage that DOMDocument does.

Encoding Fix

DOMDocument notoriously doesn’t handle encoding (at least UTF-8) correctly and garbles the output.

SmartDOMDocument tries to work around this problem by enhancing loadHTML() to deal with encoding correctly. This behavior is transparent to you – just use loadHTML() as you would normally.

Continue reading →