Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@masakielastic
masakielastic / utf8_ord.php
Created September 1, 2017 21:48
utf8_ord
<?php
function utf8_ord($char) {
$x = ord($char[0]);
if ($x < 0x80) {
return $x;
} else if ($x < 0xE0) {
$y = ord($char[1]);
<?php
$a = chr(0xfa).chr(0x4a);
var_dump(
"fa4a" === bin2hex($a),
"8754" === bin2hex(mb_strtolower($a, "CP932")),
"8754" === bin2hex(mb_strtoupper($a, "CP932"))
);
<?php
$a = chr(0xfa).chr(0x4a);
var_dump(
"fa4a" === bin2hex($a)
);
$a = mb_convert_encoding($a, "UTF-8", "CP932");
$a = mb_convert_encoding($a, "CP932", "UTF-8");
// http://stackoverflow.com/a/14947838/531320
const fromId = document.getElementById.bind(document);
const snabbdom = require('snabbdom');
const patch = snabbdom.init([
require('snabbdom/modules/class'),
require('snabbdom/modules/props'),
require('snabbdom/modules/style'),
require('snabbdom/modules/eventlisteners'),
]);
@masakielastic
masakielastic / benchmark.php
Last active August 29, 2015 14:27
mt_rand vs chr vs string concatenation
<?php
function timer(callable $block) {
$start = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
$block();
}
$end = microtime(true);
return $end - $start;
}
@masakielastic
masakielastic / benchmark.php
Last active February 10, 2022 07:37
random_int vs mt_rand
<?php
function timer(callable $block) {
$start = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
$block();
}
$end = microtime(true);
return $end - $start;
}
@masakielastic
masakielastic / index.php
Created August 1, 2015 14:08
Facebook PHP SDK v4.0 でユーザーのメールアドレスを取得する
<?php
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.4',
]);
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
$response = $fb->get('/me?locale=en_US&fields=name,email');
@masakielastic
masakielastic / index.html
Created August 1, 2015 09:43
Facebook JavaScript SDK でメールアドレスを取得。 FB.api で fields を指定する。
<!DOCTYPE html>
<html>
<head>
<title>Facebook のログインサンプル (JavaScript)</title>
<meta charset="UTF-8">
</head>
<body>
<script>
window.fbAsyncInit = function() {
FB.init({
@masakielastic
masakielastic / index.php
Created July 27, 2015 11:17
textarea の練習。投稿内容をテキストファイルとして保存。
<?php
if (isset($_POST['message'])) {
file_put_contents('db.txt', $_POST['message']);
$text = $_POST['message'];
} else {
$text = file_get_contents('db.txt');
}
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');