検索
ホームページphp教程php手册.htgroup ファイルの解析に使用される PHP クラス

.htgroup 文例:
admin: user2
editor: user1 user3
writer: user3

复制代码代码如下:


class Htgroup {
private $file = '';
プライベート関数 write($groups = array()) {
$str = '';
foreach ($groups as $group => $users) {
$users_str = '';
foreach ($users as $user) {
if (!empty($users_str)) {
$users_str .= ' ';
}
$users_str .= $user;
}
$str .= "$group: $users_strn";
}
file_put_contents($this -> file, $str);
}
プライベート関数 read() {
$groups = array();
$groups_str = file($this -> ファイル, FILE_IGNORE_NEW_LINES);
foreach ($groups_str as $group_str) {
if (!empty($group_str)) {
$group_str_array =explode(': ', $group_str);
if (count($group_str_array) == 2) {
$users_array =explode(' ', $group_str_array[1]);
$groups[$group_str_array[0]] = $users_array;
}
}
}
$groups を返します。
}
パブリック関数 __construct($file) {
if (file_exists($file)) {
$this ->ファイル = $ファイル;
} else {
die($file." が存在しません。");
false を返します。
}
}
パブリック関数 addUserToGroup($username = '', $group = '') {
if (!empty($username) && !empty($group)) {
$all = $this ->読む();
if (isset($all[$group])) {
if (!in_array($username, $all[$group])) {
$all[$group][] = $username ;
}
} else {
$all[$group][] = $username;
}
$this ->書き込み($all);
} else {
return false;
}
}
public function deleteUserFromGroup($username = '', $group = '') {
$all = $this ->読む();
if (array_key_exists($group, $all)) {
$user_index = array_search($username, $all[$group]);
if ($user_index !== false) {
unset($all[$group][$user_index]);
if (count($all[$group]) == 0) {
unset($all[$group]);
}
$this ->書き込み($all);
}
} else {
return false;
}
}
}


复制代代码如下:


$groupHandler = new Htgroup('/home/myuser/.htgroup');
// ユーザー「user1」を .htgroup のグループ「admin」に追加します。グループが存在しない場合は自動的に作成されます。
$groupHandler -> addUserToGroup('user1', 'admin');
// .htgroup のグループ「admin」からユーザー「user1」を削除します。グループにユーザーが含まれていない場合、グループは自動的に削除されます。
$groupHandler -> deleteUserFromGroup('user1', 'admin');

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)