「JavaScript」対応ブラウザ(ie、chrome、firefox)を判定するサンプルコード

サンプルコード
var client = function(){
var engine = {
ie:0,
gecko:0,
webkit:0,
khtml:0,
opera:0,
ver:null
};
var browser = {
ie: 0,
firefox: 0,
safari: 0,
konq: 0,
opera: 0,
chrome: 0,
ver:null
};
var ua = navigator.userAgent;
if(window.opera){
engine.ver = browser.ver = window.opera.version();
engine.opera = browser.opera = parseFloat(engine.ver);
} else if(/AppleWebKit/(S+)/.test(ua)){
engine.ver = RegExp[“$1”];
engine.webkit = parseFloat(engine.ver);
// Chrome/Safari判定
if (/Chrome/(S+)/.test(ua)){
browser.ver = RegExp[“$1”];
browser.chrome = parseFloat(browser.ver);
} else if (/Version/(S+)/.test(ua)){
browser.ver = RegExp[“$1”];
browser.safari = parseFloat(browser.ver);
} else {
//バージョン確認
var safariVersion = 1;
if (engine.webkit < 100){
safariVersion = 1;
} else if (engine.webkit < 312){
safariVersion = 1.2;
} else if (engine.webkit < 412){
safariVersion = 1.3;
} else {
safariVersion = 2;
}
browser.safari = browser.ver = safariVersion;
}
}else if (/KHTML/(S+)/.test(ua) || /Konqueror/([^;]+)/.test(ua)){
engine.ver = browser.ver = RegExp[“$1”];
engine.khtml = browser.konq = parseFloat(engine.ver);
}else if (/rv:([^)]+)) Gecko/d{8}/.test(ua)){
engine.ver = RegExp[“$1”];
engine.gecko = parseFloat(engine.ver);
// Firefox判定
if (/Firefox/(S+)/.test(ua)){
browser.ver = RegExp[“$1”];
browser.firefox = parseFloat(browser.ver);
}
}else if (/MSIE ([^;]+)/.test(ua)){
engine.ver = browser.ver = RegExp[“$1”];
engine.ie = browser.ie = parseFloat(engine.ver);
}
return {
engine:engine,
browser: browser
};
}();

if (client.engine.webkit) { //if it’s WebKit
if (client.browser.chrome){
//chrome some code
}
} else if (client.engine.gecko){
if (client.browser.firefox){
// Firefox some code
} else {
// Gecko some code
}
}

「Java」Collections.synchronizedList() のサンプルコード

javaコード
public class SynchronizedListDemo {

public static void main(String[] args) {
List<String> cft =
Collections.synchronizedList(new ArrayList<String>());

//Adding elements to synchronized ArrayList
cft.add(“Data AA”);
cft.add(“Data BB”);
cft.add(“Data CC”);
synchronized(cft) {
Iterator<String> iterator = cft.iterator();
while (iterator.hasNext())
System.out.println(iterator.next());
}
}
}

実行結果
Data AA
Data BB
Data CC

「PHP」デバッグに便利なメソッド–debug_backtrace()

phpコード
function loginfo($format) {
$args = func_get_args();
array_shift($args);
$d = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1)[0];
$info = vsprintf($format, $args);
$data = sprintf(“%s %s,%d: %s\n”, date(“Ymd His”), $d[“file”], $d[“line”], $info);
file_put_contents(__DIR__.”/newslog.txt”, $data, FILE_APPEND);
}

「Java」TreeMapの使い方

Javaコード
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TreeMapDemo {

public static void main(String args[]) {
/* This is how to declare TreeMap */
TreeMap<Integer, String> tmap =
new TreeMap<Integer, String>();

/*Adding elements to TreeMap*/
tmap.put(1, “AA”);
tmap.put(12, “BB”);
tmap.put(30, “CC”);
tmap.put(5, “DD”);
tmap.put(3, “EE”);

/* Display content using Iterator*/
@SuppressWarnings(“rawtypes”)
Set set = tmap.entrySet();
@SuppressWarnings(“rawtypes”)
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
@SuppressWarnings(“rawtypes”)
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print(“key is: “+ mentry.getKey() + ” & Value is: “);
System.out.println(mentry.getValue());
}

}

}
実行結果
key is: 1 & Value is: AA
key is: 3 & Value is: EE
key is: 5 & Value is: DD
key is: 12 & Value is: BB
key is: 30 & Value is: CC

「PHP」PDOを使ってMySQLのトランザクションを制御するコード

PHPコード
$servername=”172.17.1.200″;
$username=”root”;
$password=”123456″;
$dbname=”demodb”;
try{
$conn=new PDO(“mysql:host=$servername;dbname=$dbname”,$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//トランザクション開始
$conn->beginTransaction();
$conn->exec(“INSERT INTO `infotbl(`firstname`,`lastname`,`email`)VALUES(‘YAMADA’,’TARO’,’test001@sample.com’)”);
$conn->exec(“INSERT INTO `infotbl(`firstname`,`lastname`,`email`)VALUES(‘SUZUKI’,’HONG’,’test002@sample.com’)”);
$conn->exec(“INSERT INTO `infotbl(`firstname`,`lastname`,`email`)VALUES(‘WUEMURA’,’JIRO’,’test003@sample.com’)”);
//トランザクションコミット
$conn->commit();
echo “insert records successfully!”;
}catch(PDOException $e){
/トランザクションロールバック
$conn->rollBack();
echo $sql.”<br>”.$e->getMessage();
}
$conn=NULL;

「php開発」mPDF、imagickを用いてpdfからpngに変換するコード

PHPコード
function pdf2png($PDF, $PNG, $w=50, $h=50){
if(!extension_loaded(‘imagick’)){
return false;
}
if(!file_exists($PDF)){
return false;
}

$im = new Imagick();

$im->setResolution($w,$h);
$im->setCompressionQuality(15);

$im->readImage($PDF);
$im -> resetIterator();
$imgs = $im->appendImages(true);
$imgs->setImageFormat( “png” );
$img_name = $PNG;
$imgs->writeImage($img_name);
$imgs->clear();
$imgs->destroy();
$im->clear();
$im->destroy();

return $img_name;
}

「Java入門」Java.util.ArrayDeque.clear() のサンプルコード

Javaコード
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {

public static void main(String[] args) {
// create an empty array deque with an initial capacity
Deque<Integer> cft = new ArrayDeque<Integer>(8);

// use add() method to add elements in the deque
cft.add(44);
cft.add(35);
cft.add(24);
cft.add(88);

// let us print all the elements available in deque
for (Integer number : cft) {
System.out.println(“Number = ” + number);
}

// finding size of this deque
int k = cft.size();
System.out.println(“Befor deque consists of “+ k +” elements”);

System.out.println(“Performing clear operation !!”);
cft.clear();
k = cft.size();
System.out.println(“After deque consists of “+ k +” elements”);
}
}
実行結果
Number = 44
Number = 35
Number = 24
Number = 88
Befor deque consists of 4 elements
Performing clear operation !!
After deque consists of 0 elements

ubuntu12.04にLAMPサーバ(apache+mysql+php)をインストールするメモ

1.MySQLのインストール
#sudo apt-get install mysql-server
#sudo apt-get install mysql-client
#sudo apt-get install libmysqlclient15-dev

2.apache2のインストール
#tar zxvf  httpd-2.2.24.tar.gz
#sudo mv -r httpd-2.2.24 /usr/src/apache2
#cd /usr/src/apache2/httpd-2.2.24
#./configure -prefix=/usr/local/apache  -enable-module=so  -enable-rewrite=shared -enable-authn-dbm

#make install

#vim /usr/local/apache2/conf/httpd.conf
ServerName localhost:80

apache起動
#cd /usr/local/apache2/bin
# sudo ./apachectl start
3.PHP5のインストール
#sudo apt-get install libxml2-dev
# tar zxvf php-5.4.15.tar.gz
#sudo mv -r php-5.4.15  /usr/src/php5

phpのコンパイル
#cd /usr/src/php/php-5.4.15
# ./configure
-prefix=/usr/local/php5
-with-apxs2=/usr/local/apache2/bin/apxs
-with-mysql=/usr/
-with-mysqli=/usr/bin/mysql_config
-with-gd
-with-pear
-with-libxml-dir
#make
#make install

「Java」java.util.TreeMap.putAll()の使い方

javaコード
import java.util.*;

public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
TreeMap<Integer, String> tmp = new TreeMap<Integer, String>();

treemap.put(2, “大崎”);
treemap.put(1, “品川”);
treemap.put(3, “大井町”);
treemap.put(6, “川崎”);
treemap.put(5, “横浜”);

tmp.put(1, “changfa01”);
tmp.put(2, “changfa02”);
tmp.put(73, “changfa03”);

System.out.println(“変更前: “+ treemap);
treemap.putAll(tmp);
System.out.println(“変更後: “+ treemap);
}
}

「java」HttpURLConnectionクラスと getInputStream()の使い方

javaコード
private InputStream openInputStream() throws IOException{
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(“GET”);
//ヘッダ設定
con.setRequestProperty(“RANGE”, “bytes=”+this.startSize+”-“+(this.endSize));
System.out.println(con.getRequestProperty(“RANGE”));
return con.getInputStream();
}