php - How to switch Post Method to file_get_contents and file_put_contents? -
i got text file (testfile.txt) containing
127.0.0.1:80 127.0.0.1:90 127.0.0.1:100 127.0.0.1:230 127.0.0.1:110 127.0.0.1:200 127.0.0.1:201 127.0.0.1:45 127.0.0.1:86 (...) in order fix lines such 127.0.0.1:100 127.0.0.1:230 , 127.0.0.1:201127.0.0.1:45 i'm using script (post method):
$listvalue = ""; if($_server['request_method'] == 'post') { if(!empty($_post['list'])) $res = preg_match_all("/\d+\.\d+\.\d+\.\d+\:\d+/", $_post['list'], $match); if($res) foreach($match[0] $value) $listvalue .= $value."\n"; and on submit:
echo trim($listvalue); script returns:
127.0.0.1:80 127.0.0.1:90 127.0.0.1:100 127.0.0.1:230 127.0.0.1:110 127.0.0.1:200 127.0.0.1:201 127.0.0.1:86 (...) i need script read testfile.txt , save testfile.txt. ideas? cheers
this should want:
$list = file_get_contents('testfile.txt'); $res = preg_match_all("/\d+\.\d+\.\d+\.\d+\:\d+/", $list, $match); if($res) { foreach($match[0] $value) $listvalue .= $value."\n"; file_put_contents('testfile.txt', trim($listvalue)); } edit: also, instead of regexp, simple str_replace(' ', "\n", $list)
Comments
Post a Comment