Archive for the ‘PHP’ Category.

Light PHP Frameworks

I have this thing with light and fast things….I love performance!

The thing is that I use my own framework to develop PHP, but what is a framework exactly? when is a framework or just a set of utils?

My point is that I don’t like frameworks such as Zend or others that are heavy, loaded with stuff I will never use, of course this can be great for backends where there’s lack of performance since there’s not many users…I’m talking about the scenario of front end.

In front end architecture the king is the MVC design pattern. Why not just create a bunch of utilities to have an easy fast MVC and then add the classes or functions that you need?

For this things can be great any of this solutions:

Fat Free: Light MVC framework with many extra functionality in just few KB, take a look, really good documentation. Just I don’t like too much the templating feature since PHP it’s actually a template engine it self, don’t forget that…

Miminal PHP MVC: I think the name it’s enough to know what’s about. This is more or less what I’m using with more functionality that I use in my projects, but the concept it self is great, MINIMAL.

The no-framework PHP MVC framework: This is more an article written by Rasmus Lerdorf (creator of PHP) regarding frameworks. Really interesting reading.

I still don’t have my framework clean enough to make it public but I just would like to share something really simple but at the same time really useful as it is the folder structure for my projects:
Continue reading ‘Light PHP Frameworks’ »

Generating a unique Key

I need a unique Key to use to identify an object.

I use this in PHP:

$key = md5(uniqid(mt_rand(), false));

This should be unique, but can you believe that will never be repeated? I don’t trust it…

Just check it in your DB just in case:
Continue reading ‘Generating a unique Key’ »

Check if URL exists and is Online – PHP

Imagine you need to check if a site is online or not, seems pretty easy since there’s plenty of tools to check this, but this can be a huge bottleneck for your app.

I have tried different ways with sockets, header and curl in order to know which one is the fastest option.

Tested code done to the site yahoo.com (10 attempts each and we keep best result as time):
Continue reading ‘Check if URL exists and is Online – PHP’ »

Optimize your DB from PHP

This is a functionality I’ve been using in Open Classifieds since the 1.6.1. version.

It currently works for MySam working as follows:

  • If the table has deleted or split rows, repair the table.
  • If the index pages are not sorted, sort them.
  • If the table’s statistics are not up to date, update them.

And for InnoDB, rebuilds the table to update index statistics and free unused space.

And if we want to do this in PHP for all the tables in your DB simply do this:

    $result  = mysql_query('SHOW TABLE STATUS FROM '. DB_NAME); 
    while ($row = mysql_fetch_array($result)) $tables[]=$row[0];  
    $tables=implode(", ",$tables); 
    mysql_query('OPTIMIZE TABLE '.$tables);
    echo "<h3>Optimized all the tables found in the database: $tables</h3>";

That’s all! simple yet effective ;)

Extract images from HTML

Small script to extract the images from a text or HTML.

$html=file_get_contents("http://neo22s.com");
$imgsrc_regex = '!http://.+\.(?:jpe?g|gif|png)!Ui';
preg_match($imgsrc_regex, $html, $matches);
print_r ($matches);
Follow me