Forwarding example to force www

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Forwarding example to allow only two IP addresses

RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.1$ [OR]
RewriteCond %{REMOTE_ADDR} ^192\.168\.0\.2$
RewriteRule .? - [S=1]
RewriteRule ^ http://public.site.com/ [F,L]

Sample script to resize an image list

for l in `ls *.jpg`; do convert $l -size=100x100 $l; done

Sample script to delete messages older than a year in an imap account

find ~/imap -type f -mtime +365 -wholename '*/cur/*' -exec rm {} \;

Sample script used for renaming (name + content of files)

#!/bin/bash

# Validates that there are three arguments
if [ ! -n "$1" ] || [ ! -n "$2" ] || [ ! -n "$3" ]
then
      echo -e "Usage: `basename $0`\n\t Replace  'argument 1' by 'argument 2' in the folder 'argument 3'"
            exit 101
fi

recursion(){
  # Capitalize words to change
  m1=`echo ${1:0:1} | tr a-z A-Z`${1:1}
  m2=`echo ${2:0:1} | tr a-z A-Z`${2:1}
  # Uppercase the wholeword to change
  M1=`echo $1 | tr a-z A-Z`
  M2=`echo $2 | tr a-z A-Z`

  # If it is a folder
  if [ -d $3 ]
     then
       # It creates the directory copy
       mkdir `echo $2"/"$3 | sed -e "s%$1%$2%g" | sed -e "s%$m1%$m2%g"`

       # We loop files in this directory
         for l in `ls $3`
         do
           recursion $1 $2 $3"/"$l
         done
  # If it is a file
  else
    # We rename the file and change all occurrences of the word replaced
    # (as well as lowercase, uppercase and capitalized )
    sed -e "s%$1%$2%g" $3 | sed -e "s%$m1%$m2%g" | sed -e "s%$M1%$M2%g" > `echo $2"/"$3 | sed -e "s%$1%$2%g" | sed -e "s%$m1%$m2%g"`
  fi
}

# If the third parameter exists ( the file / folder renaming )
if [ -e $3 ]
then
  # Capitalize words to change
  m1=`echo ${1:0:1} | tr a-z A-Z`${1:1}
  m2=`echo ${2:0:1} | tr a-z A-Z`${2:1}

  # Create a root folder for copying
    mkdir $2
  # The term is changed in the root folder
    mkdir `echo $2"/"$3 | sed -e "s%$1%$2%g" | sed -e "s%$m1%$m2%g"`

  # We launch the recursion on the folder
  for l in `ls $3`
    do
        recursion $1 $2 $3"/"$l
     done

# The third arguement is not a folder
else
    echo -e "Usage: `basename $0`\n\t Remplace  'argument 1' par 'argument 2' dans le dossier 'argument 3'"
    echo -e "\t 'argument 3' is not a folder..."
    exit 102
fi

Script used to open/close my wifi router

var casper = require('casper').create();

casper.start('http://192.168.0.1/index.php', function(){

    // Login into the router interface
    if(this.exists("input[name='LOGIN_USER']")){
        this.fill('form#frm', {
            'LOGIN_USER': '********'
            'LOGIN_PASSWD': '********'
        }, true);

        this.click('form#frm input[type="submit"]');
    }
});
// Open the wifi management page
casper.thenOpen('http://192.168.0.1/bsc_wlan.php');

casper.then(function(){

    if(
        // Test that the submit button exist
        this.exists("input[name='enable']") &&
        (
            (
            // If the script argument is open and the status is close
            casper.cli.args[0] == "open" &&
            this.getPageContent().indexOf('f.enable.checked = false') > 0
            )
        ||
            (
            // If the script argument is close and the status is open
            casper.cli.args[0] == "close" &&
            this.getPageContent().indexOf('f.enable.checked = true') > 0
            )
        )
    ){
        // We change the wifi state
        this.click('#enable');
        this.click('input[name="apply"]');
    }

})

// We close the session for security
casper.thenOpen('http://192.168.0.1/logout.php');

casper.run();
                    

PHP

Age calculation example

    $date = new DateTime("1979-04-29");
    $now = new DateTime;

    echo $now->diff($date)->y . ' years';

SQL query via php

// Connecting to the database
try {
    $bdd = new PDO('mysql:host=localhost;dbname=####', '####', '####');
} catch(Exception $e) {
    exit('Unable to connect to database.');
}

// Query to perform
$requete = "SELECT * FROM participants order by convert(firstname using latin1) collate latin1_swedish_ci";

// SQL query execution
$resultat = $bdd->query($requete) or die(print_r($bdd->errorInfo()));

// Runs the SQL query results
while($donnees = $resultat->fetch(PDO::FETCH_ASSOC)) {
    $suggestions['suggestions'][] = utf8_encode($donnees['title']);
}
// Returns the data in JSON format
echo json_encode($suggestions);
                

Find all src attributes in a page

src="([^"]*)"

Find all src attributes in a page which are extern links

src="(?=http)([^"]*)"

SQL

Request with custom sorting

SELECT name, category FROM persons ORDER BY
       Case categorie
          when 1 then 1
          when 0 then 2
          else 3
       end,name

Find duplicates in a table

SELECT COUNT(name), name FROM persons GROUP BY name HAVING COUNT(name)>1

Get in a single field person list

SELECT GROUP_CONCAT(name) As list FROM persons WHERE membre = '1'