Category Archives: Uncategorized

Array as parameters in a PHP function

Sample PHP function that uses an array to handle arguments: function testFunction($args) { // $args contains the arguments of the function print_r($args); // Validate and assign params to variables $name = $args[‘name’] ? $args[‘name’] : "None"; $file = $args[‘file’] ? $args[‘file’] : "None"; $title = $args[‘title’] ? $args[‘title’] : "None"; }function testFunction($args) { // $args […]

Change Bootstrap Table Border Color

Change default border color of table-bordered class: .table-bordered { border: 1px solid #ecf0f1 !important; } .table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr […]

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; // […]