Category Archives: Tips

Ubuntu – How to Install MKVToolNix (Matroska tools) in Linux

Matroska is a new multimedia container format, based on EBML (Extensible Binary Meta Language), which is a kind of binary XML. MKVToolNix is cross-platform tools for Matroska.

MKVToolnix recently released its 5.3.0 verison with following changes:

1. Several fields have been added to mkvmerge’s verbose identification output.
2. File type identification and a segfault have been fixed for DTS files.
3. File type detection for (E)AC3 files has been improved.
4. mkvextract’s “timecodes_v2″ mode uses the same track IDs as mkvmerge’s identification mode outputs again.
5. An invalid memory access with certain broken Matroska files has been fixed.
6. mkvmerge can read input files as if they were part of a single huge file.
7. mkvmerge can extract and use the audio encoder delay information stored in MP4 files as written by iTunes

To Install MKVToolNix in Ubuntu:

First to import public GPG key with this command:

wget -O - http://www.bunkus.org/gpg-pub-moritzbunkus.txt | sudo apt-key add -

Then edit the source by:

gksudo gedit /etc/apt/sources.list

Read more »

PHP – Magic Constants

PHP provides useful magic constants for fetching the current line number (__LINE__), file path (__FILE__), directory path (__DIR__), function name (__FUNCTION__), class name (__CLASS__), method name (__METHOD__) and namespace (__NAMESPACE__).

We are not going to cover each one of these in this article, but I will show you a few use cases.

When including other scripts, it is a good idea to utilize the __FILE__ constant (or also __DIR__ , as of PHP 5.3):


// this is relative to the loaded script's path
// it may cause problems when running scripts from different directories
require_once('config/database.php');

// this is always relative to this file's path
// no matter where it was included from
require_once(dirname(__FILE__) . '/config/database.php');

Using __LINE__ makes debugging easier. You can track down the line numbers:


// some code
// ...
my_debug("some debug message", __LINE__);
/* prints
Line 4: some debug message
*/

// some more code
// ...
my_debug("another debug message", __LINE__);
/* prints
Line 11: another debug message
*/

function my_debug($msg, $line) {
echo "Line $line: $msg\n";
}

PHP – Memory Usage Information

By observing the memory usage of your scripts, you may be able optimize your code better.

PHP has a garbage collector and a pretty complex memory manager.

The amount of memory being used by your script. can go up and down during the execution of a script. To get the current memory usage, we can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.

echo "Initial: ".memory_get_usage()." bytes \n";
/* prints
Initial: 361400 bytes
*/

// let's use up some memory
for ($i = 0; $i < 100000; $i++) {
$array []= md5($i);
}

// let's remove half of the array
for ($i = 0; $i < 100000; $i++) {
unset($array[$i]);
}

echo "Final: ".memory_get_usage()." bytes \n";
/* prints
Final: 885912 bytes
*/

echo "Peak: ".memory_get_peak_usage()." bytes \n";
/* prints
Peak: 13687072 bytes
*/

PHP – Functions with Arbitrary Number of Arguments

You may already know that PHP allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments.

First, here is an example with just optional arguments:

// function with 2 optional arguments
function foo($arg1 = '', $arg2 = '') {

echo "arg1: $arg1\n";
echo "arg2: $arg2\n";

}
foo('hello','world');
/* prints:
arg1: hello
arg2: world
*/

foo();
/* prints:
arg1:
arg2:
*/

Now, let’s see how we can build a function that accepts any number of arguments. This time we are going to utilize func_get_args():

Read more »

PHP – Random string with numbers and letters

I thought that some of you might find it useful to learn how to generate a random string or a random number with PHP. I wrote a quick function to use PHP to generate random. See it below :

function genRandomString() {
$length = 10;
$characters = ’0123456789abcdefghijklmnopqrstuvwxyz’;
$string = ”;

for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}

return $string;
}