Archive for November, 2008

Web Development with UTF8

Sunday, November 16th, 2008

There are two mappings: the numerical value corresponding to a character (charset) and the binary representation of that value (encoding). UTF is a charset that has a number of encodings; utf8 is an encoding.
Thanks to utf, we can have different languages on the same webpage.

First of all, make sure the encoding of your text editor is utf8.

PHP

header("Content-Type: text/html; charset=utf-8");

You don’t need that if you managed to edit your httpd.conf properly.
As long as you don’t want to mess with the content of a string, you can pass it around blindly. But as soon as you want to use any function that relies on substring operations (such as substring, wordwrap, chunk_split) you need to use the equivalent multibyte functions, obtaining prepending mb_ (you need to install the extension on PHP)

MySQL

Similar to PHP (careful with substring and fulltext indexing) but no problem if the charset of the db is UFT8

Email

Similar to HTML, just use:
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />
The problem is that that meta applies only to the body but it’s quite likely you need UTF8 also for the to/from header. In that case you need a trick you can find on Google

HTML

  • Don’t forget the following in your head section:
    <meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />
  • Javascript

    It fully supports UTF8 apart from the escape() function. Google the utf8 version if you need it.

    User Input

    No problem with the user input because the browser sends data back to the server using the same encoding the server is using. But there are some exceptions (very old browsers, hacking attempts,…)

    PHP locale localization

    Saturday, November 15th, 2008

    gettext (an alias is just underscore!)

    Linux - How To Split a Big File Into Several Small Files

    Wednesday, November 12th, 2008
    split –bytes=1m /path/to/large/file /path/to/output/file/prefix

    You can change the output file size by changing the –bytes=1m to your preference. You can use b, k, or m. b represent bytes, k represent kilobytes, m represent megabytes.

    To restore the original file:

    cat prefix* > newfilename

    To split a file using a regular expression:
    csplit filename separator ‘{*}’ (the last bit is to split not just once but as many times as possible)
    To restore the original files:
    cat $(ls -L) >> a

    Rsync - Deploy And Increment Backup

    Friday, November 7th, 2008

    It’s good for deploy as well.
    It can be used also to do backups locally.
    Very useful to move big amount of data over the Internet because of its incremental feature.

    Backing up from local machine to remove backup server (with the same user):

    #!/bin/bash
    export RSYNC_RSH=/usr/bin/ssh
    dest=backup1
    user=$(whoami)
    cd || exit 1
    rsync -aHPvz . "${user}@${dest}:."
    

    [-z -> to compress, not necessary in LAN]
    [-b -> backup of the already exisitng destination files]
    SSH is useful just if you send data over the Internet.

    Listing the files on the backup server:
    #!/bin/bash
    dest=server1
    user=$(whoami)
    cd || exit 1
    rsync “${user}@${dest}:.” | more

    Restoring (the script runs locally):
    #!/bin/bash
    dest=server1
    user=$(whoami)
    cd || exit 1
    for file in “$@” ; do
    rsync -aHPvz “${user}@${dest}:./${file}” “./${file}”
    done

    For restoring, you simply run the script, passing the names of the files to be restored as arguments on the command line.
    We can also restore all the files at once by using a dot as the filename

    Linux Command Line - Bash Scripting - Some tricks

    Wednesday, November 5th, 2008

    wc - print newline, word, and byte counts for each file (program for count statistics from a file)

    dt = $(date)

    ls -l > files.txt 2>error.txt
    Redirects standard error to a file

    ls -l > files.txt 2>&1
    Redirects standard error and standard output to the same file

    Some ‘magic’ variables:

    • $? is a variable that contains the return status of the most recent command executed.
    • $$ is a variable that contains the process ID. This can be useful to create a unique temporary filename.
    • $0 is the name of the script itself
    • $* contains all the arguments as one string value
    • $@ contains all the arguments as an array -> you can use:
      for file in “$@”; do
      rm file
      done

    Apache - Easy Benchmark

    Tuesday, November 4th, 2008

    ab -n 1000 -c 5 http://mydomain.com/bench.htm
    makes 1000 separate requests for the same file with a concurrency (simultaneous requests) of 5

    PHP Profiling - Profile / Performance

    Sunday, November 2nd, 2008

    XDebug2 + KcacheGrind