ホームページ > バックエンド開発 > PHPチュートリアル > PHPでファイルの内容を取得する方法

PHPでファイルの内容を取得する方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
リリース: 2016-06-23 13:21:19
オリジナル
1145 人が閲覧しました


「ip----ドメイン名」形式のデータが含まれる txt ファイル。実装方法:
xx.com/?ip=xxx.xxx.xxx.xxx を取得して表示します。 ip----ドメイン名 このドメイン名、xxx.com
逆に、xx.com/?domain=xxx.com とすると、対応するip----ドメイン名xxxのipを取得して表示します。 xxx.xxx.xxx
これは達成できますか?


Reply Discussion (solution)

ファイルの内容を取り出し、行の変更と分離者データに応じてファイルの内容を取り出します

具体的にどうやって実装するか書いてみましたがうまくいきませんでした

もう少し詳しく説明してください! ! !

もう少し詳しく! ! !

txt ファイルには、「ip----ドメイン名」の形式のデータが含まれています。 実装方法:

各データの IP とドメイン名は相互に対応しています。

Web ページ $_GET['ip '] は、対応する IP ドメイン名を取得して表示します
逆に、Web ページ $_GET['domain'] は、対応するドメイン名の IP を取得して表示します
以下のようなデータ:
1.2.3.4-- --abc.com
5.8.6.9----dfg.com
· · · · · ·
ウェブページから ip を取得します (例: ?ip=1.2.3.4)。echo に対応するドメイン名は abc です。 com
Web ページからドメインを取得します (例: ?domain=dfg.com)、エコーに対応する IP は 5.8.6.9 です
それは達成できますか?

text.txt
192.168.1.2---xxx.com192.168.1.3---xx.com
ログイン後にコピー
ログイン後にコピー

test.php

$ip = isset($_GET['ip'])? $_GET['ip'] : '';$domain = isset($_GET['domain'])? $_GET['domain'] : '';if($ip=='' && $domain==''){    exit('ip and domain is empty');}$file = 'test.txt';$filedata = file_get_contents($file);$arr1 = array();$arr2 = array();$data = explode("\n", $filedata);foreach($data as $k=>$v){    $tmp = explode('---', $v);    $arr1[$tmp[0]] = $tmp[1];    $arr2[$tmp[1]] = $tmp[0];}if($ip!=''){    if(isset($arr1[$ip])){        echo $ip.'---'.$arr1[$ip];    }else{        echo 'ip not exists';    }}elseif($domain!=''){    if(isset($arr2[$domain])){        echo $arr2[$domain].'---'.$domain;    }else{        echo 'domain not exists';    }}
ログイン後にコピー
ログイン後にコピー


http://xxx.com/test.php?ip=192.168.1.2
http://xxx.com/test.php?domain=xxx。 com


text.txt

192.168.1.2---xxx.com192.168.1.3---xx.com
ログイン後にコピー
ログイン後にコピー

test.php

$ip = isset($_GET['ip'])? $_GET['ip'] : '';$domain = isset($_GET['domain'])? $_GET['domain'] : '';if($ip=='' && $domain==''){    exit('ip and domain is empty');}$file = 'test.txt';$filedata = file_get_contents($file);$arr1 = array();$arr2 = array();$data = explode("\n", $filedata);foreach($data as $k=>$v){    $tmp = explode('---', $v);    $arr1[$tmp[0]] = $tmp[1];    $arr2[$tmp[1]] = $tmp[0];}if($ip!=''){    if(isset($arr1[$ip])){        echo $ip.'---'.$arr1[$ip];    }else{        echo 'ip not exists';    }}elseif($domain!=''){    if(isset($arr2[$domain])){        echo $arr2[$domain].'---'.$domain;    }else{        echo 'domain not exists';    }}
ログイン後にコピー
ログイン後にコピー


http://xxx.com/test.php?ip=192.168.1.2
http://xxx.com/test.php?domain= xxx.com

テストは完璧に機能しました。ありがとうございました!
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート