Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify the contents of txt files with php

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use php to modify the contents of txt files", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use php to modify the contents of txt files.

Php modify txt file content method: 1, create a PHP sample file; 2, open the file through fopen; 3, through "fwrite ($fp,$password);" modify the txt file content.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How does php modify the contents of txt files?

Php reads and modifies txt files

/ / there is only one line of data read in the txt file.

$fp = fopen ("password.txt", "r"); if ($fp) {$pwd = fgets ($fp);} else {echo "failed to open file";} fclose ($fp)

/ / there is only one line of data written in the txt file

$fp = fopen ("password.txt", "w"); / / write to if ($fp) {$flag=fwrite ($fp,$password); if (! $flag) {echo "failed to write to the file

"; break;}} fclose ($fp)

/ / multiple lines of data in the txt file are read in the form of an array

$path= "wifi_customer_settings.txt"; $body = file_get_contents ($path); if (file_exists ($path)) {$body = file_get_contents ($path); / / converted array / / echo "alert ('file exists');";} else {/ / echo "alert ('file does not exist $path');";} $cbody = file ($path); / / print_r ($cbody); $wifissid=$cbody [0]; $wifipwd=$cbody [1] $wifissid=str_replace ('wifiSSID=','', $wifissid); / / text replacement replace the wifiSSID of the value of $Wifissidwith' $wifipwd=str_replace ('wifiPWD=','', $wifipwd)

/ / multiple lines of data are written in an array in the txt file.

$path= "wifi_customer_settings.txt"; $wifissid2=$_POST ['wifissid']; $wifipwd2=$_POST [' wifipwd']; $arr=array ("wifiSSID=". $wifissid2, "\ n", "wifiPWD=". $wifipwd2); $fp=fopen ($path,'w'); fputs ($fp,$arr [0]); fputs ($fp,$arr [1]); fputs ($fp,$arr [2]); fclose ($fp) At this point, I believe you have a deeper understanding of "how to use php to modify the contents of txt files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report