Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Install NodeJS in Linux

Answer:

In order to install NodeJS in Linux, the recommended way is compile from source and install it.

# wget http://nodejs.org/dist/node-v0.4.11.tar.gz
# tar zxf node-v0.4.11.tar.gz
# cd node-v0.4.11
# ./configure
# sudo make && make install

Now you can type node to enter the node's REPL

How to remove object from array inside foreach loop?

Answer:

In PHP, to remove object (e.g. with the key = foo in the following example) from array inside foreach loop, you can try:

<?php

foreach($array as $key=> $value) {
    if ($key == "foo") {
        unset($array[$key]);
    }
}

Check if a file exist in Bash Shell

Answer:

The following script demonstrates how to check if a file exist, using shell (Bash) script

#!/bin/bash

if [ -e test.txt ]
then
  echo 'The file exists.'
else
  echo 'The file does not exist.'
fi

Validate IP address in PHP

Answer:

If you are using PHP 5.2 or above (in fact, at the moment of writing, you should use at least PHP 5.3+), you can use the following easy method:

if(filter_var($ip, FILTER_VALIDATE_IP))
{
  // Valid IP
}
else
{
  // Invalid IP
}

Get the current PHP include path's setting

Answer:

To get the current PHP include path's setting, you can use the function get_include_path

E.g.

echo get_include_path();

Usually it is used with the set_include_path for adding extra path to the existing include_path,

E.g.

set_include_path(get_include_path() . PATH_SEPARATOR . "/data/path");