「PHPの入門 」PHPの良く使う便利な関数コレクション

1:strip_tags
説明:
文字列からHTMLおよびphpタグを取り除く
例:
$message = “<div> 列島酷暑 熱中症に厳重警戒:<span>http://yahoo.co.jp<span> </div>";
echo strip_tags($message); //結果: “列島酷暑 熱中症に厳重警戒:http://yahoo.co.jp"

2.array_rand()
説明:配列から一つ以上の要素をランダムに取得する

$sites = [“https://arkgame.com", “http://www.google.com", “http://www.twitter.com"];
$k = array_rand($sites);
$sites[$k];

3.strftime()
説明:
ロケールの設定に基づいてローカルな日付、時間をフォーマットする
例:
strftime(“%B %d, %Y", time()); // July 28, 2024

4.range()
説明:
ある範囲の整数を要する配列を作成する
例:
range(0, 8); // array(0, 1, 2, 3, 4, 5, 6, 7, 80)
range('a’, 'f’); // array('a’, 'b’, 'c’, 'd’, 'e’. 'f’);
range(2, 10, 2); // array(2, 4, 6, 8, 10);
5.isset()
説明:変数がセットされていること、そしてnullでないことを調べる
例:$site="https://arkgame.com"
isset($site) //true
isset($name)//false

$_GETと$_POSTの指定したキーが存在するかどうかを確認
if(isset($_GET['query’])) {
// get results and display them
} else {
// show some default content
}

6.basename()
説明:
ファイルあるいはディレクトリへのパスを含む文字列を受け取って、最後にある名前の部分を返す
例:
$path = “/usr/local/apche/hp/loginday/startnews24_com.html";
$filename1 = basename($path); // startnews24_com.html
$filename2 = basename($path, “.html"); // startnews24
7.list()
説明:
配列と同様の形式で、複数の変数の代入を行う
例:
$array = [“arkgame.com", “facebook.com"];
list($first_website, $last_website) = $array;

echo $first_website; // arkgame.com
echo $last_website; // facebook.com
$data = “foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(“:", $data);

PHP

Posted by arkgame