Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

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, 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, July 20, 2011

Singleton design pattern in PHP

You can find all the information you need on the web if you google singleton design pattern. So I'm not gonna fill your head with all the theory.

I'll tell you how you can implement it.

Thursday, June 23, 2011

How to fit an image in a rectangle area in PHP using GD

Have you ever had a fixed space in your design, and you must put an image that fits exactly there, while preserving the aspect ratio? Well, this is what i'll explain in this article.

Tuesday, June 21, 2011

Using a transparent PNG image as watermark in PHP

Sure there are plenty of scripts on the internet that does this thing ... but very few explain this.