Category Archives: Uncategorized

Node.js Execute a Unix Command

Use child_process.exec to execute a comand and fetch its output: var exec = require(‘child_process’).exec; var cmd = ‘id’;   exec(cmd, function(error, stdout, stderr) { console.log("Output: "+stdout); });var exec = require(‘child_process’).exec; var cmd = ‘id’; exec(cmd, function(error, stdout, stderr) { console.log("Output: "+stdout); }); Or you can use execFile() method: var execFile = require(‘child_process’).execFile;   execFile("/bin/ls", [‘-la’], […]

Node.js Cluster Module

Example Node.js cluster module usage: var cluster = require(‘cluster’); var http = require(‘http’); var numCPUs = require(‘os’).cpus().length;   if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); }   cluster.on(‘exit’, function(worker, code, signal) { console.log(‘worker ‘ + worker.process.pid + ‘ died’); }); } else { // Workers […]

Basic usage of jQuery $.ajax

Example Form HTML code: <form id="foo"> <label for="bar">A bar</label> <input id="bar" name="bar" type="text" value="" />   <input type="submit" value="Send" /> </form><form id="foo"> <label for="bar">A bar</label> <input id="bar" name="bar" type="text" value="" /> <input type="submit" value="Send" /> </form> Javascript code: // Variable to hold request var request;   // Bind to the submit event of our form […]

Best PHP HTML\XML Parser Class

This is the fastest and simplest HTML parser class for PHP: https://github.com/Imangazaliev/DiDOM Here is an example usage: use DiDom\Document;   $document = new Document(‘http://www.news.com/’, true);   $posts = $document->find(‘.post’);   foreach($posts as $post) { echo $post->text(), "\n"; }use DiDom\Document; $document = new Document(‘http://www.news.com/’, true); $posts = $document->find(‘.post’); foreach($posts as $post) { echo $post->text(), "\n"; } […]

Regex Remove Blank Lines from Text in PHP

This regex will remove (preg_replace) all empty new lines from a text: $str = preg_replace(‘/^\n+|^[\t\s]*\n+/m’, ”, $str);$str = preg_replace(‘/^\n+|^[\t\s]*\n+/m’, ”, $str);

Javascript Get Window Location Hostname

Various example demonstration how to get URL on the address bar using Javascript, which include location.hostname, window.top.location, window.location.origin, and more. In the example below I included also an example to remove the www. from the website address. Here is the script code to get various locations with Javascript var x = location.hostname; document.getElementById("demo1").innerHTML= x; var […]

Best Google Fonts for Your Web Projects

My favorites Google Fonts for web projects are: Lato (10 Styles) Open Sans (10 Styles) Neuton (6 Styles) Montserrat (2 Styles) Source Sans Pro (12 Styles) Raleway (18 Styles) PT Sans (4 Styles) Droid Sans (2 Styles) Quicksand (4 Styles) Nunito Sans (14 Styles)

Java HashCode in PHP and JavaScript

JavaScript function of Java hashCode(): /** * @see http://stackoverflow.com/q/7616461/940217 * @return {number} */ String.prototype.hashCode = function(){ if (Array.prototype.reduce){ return this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0); } var hash = 0; if (this.length === 0) return hash; for (var i = 0; i < this.length; i++) { var character = this.charCodeAt(i); hash = ((hash<<5)-hash)+character; hash = hash & hash; // […]

Example usage PHP Namespace and Use

Pay attention to the order, namespace goes at first line: namespace LayerShifter\TLDExtract\Tests;   include_once __DIR__ . ‘/vendor/autoload.php’;   use LayerShifter\TLDDatabase\Store; use LayerShifter\TLDExtract\Exceptions\RuntimeException; use LayerShifter\TLDExtract\Extract; use LayerShifter\TLDExtract\Result;   // Below here insert custom functions etc   $extract = new Extract();   $extract->setExtractionMode(Extract::MODE_ALLOW_ICCAN);   $result = $extract->parse(‘a.b.blogspot.com’);   print_r($result->getRegistrableDomain());namespace LayerShifter\TLDExtract\Tests; include_once __DIR__ . ‘/vendor/autoload.php’; use LayerShifter\TLDDatabase\Store; use […]