Skip to content

Instantly share code, notes, and snippets.

@tohokuaiki
tohokuaiki / bash_init
Created May 2, 2021 04:02
$source bash_init することで個のファイルがあるところからnode_modules/.binもPATHに入れる
# source bash_init
SCRIPT_DIR=$(cd $(dirname $BASH_SOURCE); pwd)
export PATH="$PATH:$SCRIPT_DIR/node_modules/.bin"
<?php
/**
* 半角スペース区切りの検索ワードを複数フィールドで検索する時にありがちなEloquent
**/
$query = new QueryBuilder();
$search_query = "foo bar";
$search_fields = ['title', 'author'];
// WHERE (`title` LIKE '%foo%' or `authos` LIKE '%foo%') and (`title` LIKE '%bar%' or `authos` LIKE '%bar%')
foreach (array_map('trim', array_filter(explode(' ', $search_query))) as $word) {
$query->where(function ($query) use ($word, $search_fields) {
// i番目の要素を1つ後ろにずらす(i>0)
a = ['aaa', 'bbb', 'ccc', 'ddd'];
i = 2;
a.splice(i-1, 2, a[i], a[i-1]);
console.log(a); // ['aaa', 'ccc', 'bbb', 'ddd'];
// i番目の要素を1つ前にずらす(i>0)
a = ['aaa', 'bbb', 'ccc', 'ddd'];
i = 2;
i--;
@tohokuaiki
tohokuaiki / rsyncする時のオプション.sh
Created January 28, 2021 06:02
rsyncをshellにしておくんだけど、そのまま実行時はDRYで ./my_rsync.sh do で本実行するためのオプションをいつもどっかからこぴーするので。
#!/bin/sh
OPTION="-rltgoDvz"
if [ $# -ne 1 ]; then
ARG="dry"
else
ARG=$1
fi
@tohokuaiki
tohokuaiki / put_csv_to_stream.php
Last active January 19, 2021 05:03
PHPでCSVファイルを作成する時に、ファイルではなく直接文字列として取得したい場合
fopen('php://output', 'a+') を使いましょう。
<?php
$fields = [
'p.name' => '名前',
'p.name_yomi' => 'よみがな',
'p.email' => 'メールアドレス',
'p.created_at' => '日時',
];
ob_start();
<?php
require_once '../../work/qr-code/vendor/autoload.php';
use Endroid\QrCode\QrCode;
?>
<html>
<body>
<?php
$qr = new QrCode('Life is too short to be generating QR codes');
$qr->setWriterByName('svg');
$writer_options = [
@tohokuaiki
tohokuaiki / number_format_filesize.php
Created April 21, 2020 04:44
ファイルサイズをなんかいい感じにやってくれるやつ。
<?php
function number_format_filesize($string)
{
$size = intval($string);
$unit = "Byte";
$units = array(
1 => "B",
1000 => "kB",
1000000 => "MB",
@tohokuaiki
tohokuaiki / check_my_ipaddress.sh
Last active April 20, 2020 06:02
自分のIPアドレスをネームサーバの対象ドメインと比較してチェックするshellスクリプト
#!/bin/sh
LIVE_HOSTNAME='www.example.com'
NIC='ens3'
LIVE_IP=`nslookup $LIVE_HOSTNAME|grep "Address:"|grep -v "#53"`
LIVE_IP=`echo $LIVE_IP |sed -e "s/^Address: //"`
MYIP=`ip -f inet -o addr show $NIC|cut -d\ -f 7 | cut -d/ -f 1`
@tohokuaiki
tohokuaiki / change_apache_conf.sh
Last active April 10, 2020 12:39
メモリのサイズから、Apacheのpreforkの数を自動的に設定するbash Apacheの1プロセスが128M消費する前提
#!/bin/bash
MEMINFO=`free -m|grep "Mem:"`
MEM=(${MEMINFO// / })
MEMSIZE=${MEM[1]}
SERVER_LIMIT=$((MEMSIZE/128))
START_SERVERS=$((SERVER_LIMIT/7*5))
sed -e "s/__START_SERVERS__/$START_SERVERS/g" /root/apache2_conf/mods-enabled/mpm_prefork.conf.placehold \
@tohokuaiki
tohokuaiki / mirai-compass-rss-index.js
Last active November 18, 2019 07:07
ミライコンパスのRSSを取得します。nodeでHTTPDを起動してhttp://localhost:3000/?school=seiritzj&school=kariakej&school=kokushih&school=josogj&school=shukuynjとかにアクセスします。起動ポートは node index.js --port=3131とかで変更可
const http = require('http');
const url = require('url');
const request = require('request');
const cheerio = require('cheerio');
const minimist = require("minimist");
var LRU = require("lru-cache");
var options = minimist(process.argv.slice(2), {
string: 'port',