Skip to content

Instantly share code, notes, and snippets.

View hirak's full-sized avatar

Hiraku NAKANO hirak

View GitHub Profile
@hirak
hirak / msort.diff
Created June 29, 2011 14:33
merge sort patch
--- sort.js 2011-06-29 23:34:27.000000000 +0900
+++ sort2.js 2011-06-29 23:34:37.000000000 +0900
@@ -1,10 +1,10 @@
var sort = function(list, comparer) {
- if (!comparer) comparer = function(x, y) { return y - x; };
+ if (!comparer) comparer = function(x, y) { return x - y; };
return (function self(list) {
if (list.length < 2) return list;
var left = self(list.splice(0, list.length >>> 1)), right = self(list.splice(0));
while (left.length && right.length)
@hirak
hirak / file0.php
Created December 10, 2011 02:28
Yahoo!検索API経由でページの本文を取得するサンプル ref: http://qiita.com/items/1337
<?php
require_once 'Zend/Rest/Client.php';
//アプリケーションIDは各自で取得したものを使ってください。
$appid = 'プレミアム検索対応のアプリケーションIDをここに記入';
//APIのベースURLを指定する
$client = new Zend_Rest_Client('http://search.yahooapis.jp/PremiumWebSearchService/V1/webSearch');
//パラメータを設定
@hirak
hirak / file0.php
Created December 28, 2011 13:22
/dev/urandomで予測困難なトークンを作成 ref: http://qiita.com/items/1477
<?php
function getToken() {
$s = file_get_contents('/dev/urandom', false, NULL, 0, 24);
return base64_encode($s);
}
@hirak
hirak / file0.php
Created January 3, 2012 05:15
Zend_Db_Tableをもっと手軽に使う(概要編) #ZFhimekuri ref: http://qiita.com/items/1539
<?php
//常にZend_Db_Tableを作成するところから開始します。
$posts = AR('posts');
//テーブルクラスにデータを検索してもらったり
$latestPost = $posts->find(1)->current();
//テーブルクラスに新しいRowデータの雛形を作ってもらったり
$newpost = $posts->createRow();
$newpost->title = '新しいポスト';
@hirak
hirak / tree.php
Created January 18, 2012 17:04
RecursiveTreeIteratorでtreeコマンドを実装 ref: http://qiita.com/Hiraku/items/4da9fe08ea83773f9631
#!/usr/bin/env php
<?php
/**
* treeコマンドのPHP5.3による実装例。
*/
namespace Tree;
//iterator類は名前が長いので短縮
use RecursiveDirectoryIterator as RDI,
RecursiveFilterIterator as RFI,
@hirak
hirak / file0.php
Created February 23, 2012 23:42
シンプルなPSR-0準拠クラスオートローダー ref: http://qiita.com/items/2774
<?php
//以下、コードの先頭に書いておけば以降require_onceを書かなくてよくなる
function simpleClassLoader($classname) {
return @include_once str_replace(
array('\\', '_'),
DIRECTORY_SEPARATOR,
$classname
) . '.php';
}
@hirak
hirak / entry1.atom
Created February 24, 2012 15:18
最小限のAtomフォーマット ref: http://qiita.com/items/2790
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<id>tag:example.com,2012:1234</id>
<title>sample entry</title>
<updated>2012-01-01T00:00:00+09:00</updated>
<author><name>author name</name></author>
<content>コンテンツの中身そのもの</content>
</entry>
@hirak
hirak / file0.php
Created March 22, 2012 11:33
配列か連想配列か判定する ref: http://qiita.com/items/721cc3a385cb2d7daebd
<?php
if (array_values($arr) === $arr) {
echo '$arrは配列';
} else {
echo '$arrは連想配列';
}
@hirak
hirak / file0.php
Created March 26, 2012 16:44
PDOで1xNのデータをDBから取得するなら ref: http://qiita.com/items/856c4bac914451bc0c75
<?php
$pdo = new PDO('...');
$stmt = $pdo->query('SELECT txt FROM Hoge');
var_dump($stmt->fetchAll(PDO::FETCH_NUM));
//[ [txt1], [txt2], [txt3], ... ]