Thursday, July 19, 2012

Substract a string on a word boundry using regexp

There are many answers on this problem, but i found one very easy to implement, lightweight, and fast.
I think the fewer code lines, the better, so why not using a regexp instead of substr?

Wednesday, July 18, 2012

Complex regular expressions (regex) in php

Regular expressions are a very good way to check if a text meets several standards or to search for patterns.

There are several regex that are most used by programmers.

I found out that many programmers don't really understand how regex works. Sometimes, the best way to learn regex is by example.

How to add a folder to a zip archive with PHP

If you want to use ZipArchive to archive a folder, you will notice that there is no dedicated method to do this. You will need to make a recursive function to do this.

  function addDirectoryToZip(&$zip, $dir) {
    foreach(glob($dir . '/*') as $file) {
      if(is_dir($file))
        addDirectoryToZip($zip, $file);
      else
        $zip->addFile($file);
    }
  }