Thursday, April 4, 2013

Remove duplicate values from an array in PHP

A friend of mine asked me about a small problem: how to remove duplicate values from an array in PHP.

Considering the target was to use recursive function, I gave him the following code:

function removeDuplicates(&$array) {
static $newArray=array();
if(current($array)){
if(@++$newArray[current($array)] > 1) {
unset($array[key($array)]);
} else {
next($array);
}
removeDuplicates($array);
}
}

Thursday, November 1, 2012

How to change the site layout only from CSS

Nowadays, if you have a website, you have to keep in mind that your website will be visited not only from PC with "high" resolutions, but from smartphones and tablets too.
So how can you make your site appear attractive on all devices, and not use too many resources? Your answer could be CSS.

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);
    }
  }

Thursday, June 14, 2012

Perl-compatible regular expressions in PHP

Perl Compatible Regular Expressions (normally abbreviated as 'PCRE') offer a very powerful string-matching and replacement mechanism that far surpasses any other search and replace function.

Regular expressions are often thought of as very complex—and they can be at times. However, properly used they are relatively simple to understand and fairly easy to use. Given their complexity, of course, they are also much more computationally intensive than the simple search-and-replace functions. Therefore, you should use them only when appropriate—that is, when using the simpler functions is either impossible or so complicated that it’s not worth the effort.

Wednesday, March 28, 2012

Make textarea auto-height as you type using jQuery

jQuery is one of the most popular JavaScript frameworks, a fast and concise library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.