2009/10/26

window.location

function onSelectChangeSite(){
   var dropdown = document.getElementById("snssite");
   var index = dropdown.selectedIndex;
   var ddVal = dropdown.options[index].value;

   var current = window.location;
   var re1 = /\?custm/;
   var re2 = /\?/;

   if (re1.test(current))
      window.location = "?custm=" + ddVal;
   else if (re2.test(current))
      window.location = current + "&custm=" + ddVal;
   else
      window.location = "?custm=" + ddVal;

}

2009/10/25

perl note part2

1.      @line= <STDIN>

Chomp(@line)

print @line;

CTR+D in linux CTR+Z in window to end of standard input

2.      A lot of default

Sub list_from_a_to_b{

            If ($a>$b){

            $a;

            }

            Else{

            $b;

            }

}

$a, $b are global variables. No return needed

Or use

Sub list_from_a_to_b{

            ($a,$b) = @_

Or use

$a = $_[0];

$b = $_[1];

 

2009/10/23

perl note part1

Perl does not have Boolean type true/false

Chomp chops new line, but chop chops the last char

Below does not work in php ($sum UNDEF), but works in perl (unless warning/strict turn on)

$n=1;

while($n<10){

$sum += $n++;

}

print $sum;

qw shortcut

qw /fred barney betty Wilma dino/ == (“fred”, “barney” …)

@array = 6..9

Push/pop operators do things to the end of an array, but shift/unshift do things to the begin of an array

for (1..9){ print;} == for (1..9) {print $_;}

sort/reverse functions does not change the array itself, but return a sorted/reversed array

2009/10/20

more xml encode

function xml_encode($str) {
   $str = preg_replace("/".chr(153)."/","(tm)",$str); // trademark
   $str = preg_replace("/".chr(174)."\s/","(r) ",$str); // registered tratemark
   $str = preg_replace("/".chr(174)."\W?/","(r)",$str); // registered tratemark
   $str = preg_replace("/".chr(169)."/","",$str); // copyright
   $str = preg_replace("/".chr(194)."/","",$str); // Capital A, circumflex accent

   $str = preg_replace("/&reg;/","(r)",$str);
   $str = preg_replace("/&quot;/","\"",$str);
   $str = preg_replace("/&#153;/","(tm)",$str);
   return $str;
}


How to use implode in IN sql

Here is a example,

$review_id = array();
$sql = "update pr_approved set emailed = 'Y' where merchant_review_id ('".implode("','",$review_id)."')";

2009/10/14

Different between Minfier and Packer

Found this article at
http://markmail.org/message/2v5mteddw3busgbp#query:packed%20vs.%20minified+page:1+mid:nfd2zt6ub3yltzgs+state:results


SWFObject uses YUI Compression (which is basically/similar-to minified, but with additional 'intelligence' to achieve smaller but still safe compression).

----------------------------------- Though it's a little off topic, for the benefit of the readership, here's a brief discussion of the differences:

YUI Compression (and minification) essentially strips out all whitespace/comments, shortens (safely) all internal variables, and removes unnecessary code syntactics.  The benefit is really good compression, but extremely quick "initialization" (in other words, having it ready on your page), because the code is directly executable in the page.

One other downside of YUI Compressor is that it strips out (or fails on, sometimes) IE-conditional-compilation comments, which SWFObject uses.  So, SWFObject's authors actually remove the IE-conditional-compilation block, YUI compress the code, then add the block back in, adjusting it to the right minified variable names, etc.  Kind of a pain, but this produces the best result (see below).

Packing (like Dean Edwards' Packer, among others) uses a slightly different concept -- it actually applies an advanced set of 'compression' packing algorithm to the code, rearranging code blocks/commands, using techniques to take advantage of looping, etc... and then it tacks onto the end a small runtime that 'unpacks' the payload and passes it through an eval() statement to evaluate it for execution on the page. Some 'packers' even actually use real compression, like zip or gzip, with javascript implementations of the decompressors.  The benefit is much higher compression, but the tradeoff is that the "decompression"/evaluation actually has some performance cost, sometimes a few seconds worth, for it to initialize and be ready for the page to use.

One additional factor is that a server can apply gzip/deflate compression on the stream of text being sent to the browser, so a 'minified' script can be 'compressed' with gzip (usually on the fly) for transfer over-the-line, and the browser takes care of natively uncompressing it.  This type of compression has much less performance cost.  The main downside is that the server (and browser, which most modern ones do) must support gzip compression.

Generally speaking, for most scripts, YUI compression (minification) combined with server/browser-gzip transfer turns out to be the best combination -- it generally gets the best, most efficient "compression", because it achieves close to (or better) compression to 'packers', but without most of the runtime performance cost.

In fact, this URL will take any script and run it through several of the most common minifiers/packers/compressors, and their various configuration tuning options, and return a list of all combinations with size and runtime speed costs. It sorts the list so you can see the best choice for your script.  http://compressorrater.thruhere.net/

Hope that helps.  :)

--Kyle

remove file named single quote

When I use VIM, sometimes I careless enter the wrong key and saved the file using single quote as file name.
[root@myserver page_render]# ls
'                    optin.php
You see the ' as  file name above. How to delete it?

I tried:
[root@myserver page_render]# rm '
>
It failed. I knew it can be deleted thru FTP client like WinSCP, etc. But just figure out how to delete it from command line. It is easy, but you have to escape it, like following,

[root@myserver page_render]# rm \'
rm: remove regular file `\''? y


2009/10/02

how to use smarty plugin with multiple parameters

For example, you have a plugin function like,

function smarty_modifier_trans_seo($string,$grp="",$sbgrp="",$ln="") {}

As you see, this plugin function accepts up to 4 parameters. By default the last 3 are empty.

Usage:

1. one parameter:
{$onefoo|trans_seo}

2. two parameters:
{$firstfoo|trans_seo:$secondfoo}

3. there parameters:
{$firstfoo|trans_seo:$secondfoo:$thirdfoo}

Four parameters, I bet you know how to do it.