Maison >développement back-end >tutoriel php >php怎么获取文件里的内容
一个txt文件,里面有格式为 ip----域名 的数据,怎么实现:
xx.com/?ip=xxx.xxx.xxx.xxx,则获取并显示对应ip----域名的这个域名,xxx.com
反之,xx.com/?domain=xxx.com,则获取并显示对应ip----域名的这个ip,xxx.xxx.xxx.xxx
可以实现么?
取出文件的内容,根据换行跟分隔符切分数据
取出文件的内容,根据换行跟分隔符切分数据
说的稍微详细点!!!
说的稍微详细点!!!
text.txt
192.168.1.2---xxx.com192.168.1.3---xx.com
$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'; }}
text.txt
192.168.1.2---xxx.com192.168.1.3---xx.com
$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'; }}