2024年5月11日土曜日

Linux findとgrepでファイル内文字列検索

# 現在のディレクトリ以下にある「xyz*」ファイル内に「aiueo」が含まれているものを検索する

$ find . -name 'xyz*' -type f -print0 | xargs -0 grep aiueo


# 現在のディレクトリ以下にあるphpファイル内に「aiueo」が含まれているものを検索する

$ find . -name '*.php' -print0 | xargs -0 grep aiueo


# /var/www/htmlディレクトリ以下にあるファイル内に「aiueo」が含まれているものを検索する

$ find /var/www/html -type f -print0 | xargs -0 grep aiueo


2024年5月10日金曜日

PHP URL文字列の取得まとめ

URL例: https://example.co.jp/ja/company/

$_SERVER['REQUEST_URI']; 
パスとクエリ取得(ファイル名あり)
⇒ /ja/company/index.html?year=2017&page=2 

$_SERVER['QUERY_STRING']; 
 クエリ取得(?は除外)
⇒ year=2017&page=2 

$_SERVER['SCRIPT_NAME'];
クエリを除外したパス(ファイル名あり) 
⇒ /ja/company/index.html 

str_replace($_SERVER['QUERY_STRING'],"",$_SERVER['REQUEST_URI']); 
ファイル名無しパス(末尾に?が残る)
⇒ /ja/company/?

2024年5月2日木曜日

PHP 連想配列をJSONにしてHTTP送信する



  $data = array();
  $data["name"] = 'tanaka';
  $data["age"] = 30;

  // 連想配列をJSON形式に変換する
  $json_data = json_encode($data);

  // HTTPヘッダを設定してJSONデータを送信する
  header("Content-Type: application/json");
  
  echo $json_data;