If you want to exclude a directory from the archive (but include all the other content), the regular expression must take into consideration the whole path of the file, not just the file or directory name relative to the source folder.
As an example, if we want to exclude "nbproject" directory from the archive (and any occurrence of it):
/tmp/myfolder
/nbproject
/something
/something-else
/nbproject
/file1.php
/file2.php
the regex should be:
<?php
$exclude = '/^(?!(.*nbproject))(.*)$/i'; //ignoring the case
?>
A more complete example:
<?php
$archive_file = 'myarchive.tar';
$folder_to_compress = '/tmp/myfolder';
$archive = new PharData($archive_file);
$exclude = '/^(?!(.*nbproject))(.*)$/i';
$archive->buildFromDirectory($folder_to_compress,$exclude);
$archive->compress(Phar::GZ);
unlink($archive_file); // as we already obtained a tar.gz
?>
the archive will contain:
/
/something
/something-else
/file1.php
/file2.php