Home Backend Development PHP Tutorial 怎么删除txt文件中的【最后一行】?

怎么删除txt文件中的【最后一行】?

Jun 13, 2016 am 10:33 AM
fwrite quot trim txt

如何删除txt文件中的【最后一行】??
fwrite($flietxt,$txt);
fclose($flietxt);

最后一行总是空的.有时候随机选择的时候选择到最后一行总是空的容易出现错误.有什么办法可以删除掉最后一行...

------解决方案--------------------
试试这样,而且以后类似问题,建议也这样解决。

PHP code
$temp = array();while($r=$empire->fetch($sql)){        $titleurl=sys_ReturnBqTitleLink($r);        $txt=$r['id'].'||'.$r['title'].'||'.$titleurl.'||'.$r['newstime'].'||'.$r['titlepic'];        //fwrite($flietxt,$txt);          $temp[] = $text;           } fwrite($flietxt, implode("\n" , $temp ) );fclose($flietxt);<br><font color="#e78608">------解决方案--------------------</font><br>这个问题你都能弄得这么复杂。<br>唠叨的<br>fwrite($flietxt, trim($txt) );<br>你怕没有换行符<br>那你就<br>fwrite($flietxt, trim($txt)."\n" );<br><font color="#e78608">------解决方案--------------------</font><br>就是去掉txt末尾重复的\n, 楼上即可.<br><font color="#e78608">------解决方案--------------------</font><br>楼主打印$txt看看。<br><font color="#e78608">------解决方案--------------------</font><br>你问题的标题是:如何删除txt文件中的【最后一行】<br>也就只给了<br>fwrite($flietxt,$txt);	<br>fclose($flietxt);<br>两行代码<br>我回答你 fwrite($flietxt, trim($txt) );<br>有什么不妥吗?对于多行文本,只要删去最后的换行符就可以了<br><br>对于 #2 的派生问题,要具体情况具体分析<br>那是分行逐次写文件<br>如果需要最后的一行不写换行符,判断起来比较复杂<br>但如果倒过来,问题就简单了<br>
Copy after login
PHP code
$lr = '';while($r=$empire->fetch($sql)){  fwrite($flietxt, $lr . trim($txt) );  $lr = PHP_EOL;}<div class="clear">
                 
              
              
        
            </div>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
What is Intel TXT? What is Intel TXT? Jun 11, 2023 pm 06:57 PM

IntelTXT is a hardware-assisted security technology launched by Intel. It can ensure the integrity and security of the server during startup by establishing a protected space between the CPU and BIOS. The full name of TXT is TrustedExecutionTechnology, which is Trusted Execution Technology. Simply put, TXT is a security technology that provides hardware-level protection to ensure that the server has not been modified by malicious programs or unauthorized software when it is started. this one

How to convert html to txt How to convert html to txt Aug 31, 2023 am 09:23 AM

Methods for converting html to txt include using a text editor, using online conversion tools, and using Python programming. Detailed introduction: 1. To open an HTML file, you can use any text editor, such as Notepad, Sublime Text, etc. To select the content of the entire HTML file, you can press the Ctrl+A shortcut key or drag the mouse to select and copy the selection. The content can be copied by pressing the Ctrl+C shortcut or through the copy option in the right-click menu, opening a new TXT file, using the same text editor, etc.

How to convert chm to txt How to convert chm to txt Oct 17, 2023 pm 02:42 PM

chm is converted to txt by using online conversion tools, using browser plug-ins, using command line tools and using third-party software. Detailed introduction: 1. Use the online conversion tool, just upload the CHM file, select the TXT format, and then download the converted TXT file; 2. Use the browser plug-in, after installing the plug-in, just open the CHM file in the browser, and then Click the plug-in button to convert CHM files into TXT format; 3. Use command line tools, etc.

FAQ for pandas reading txt files FAQ for pandas reading txt files Jan 19, 2024 am 09:19 AM

Pandas is a data analysis tool for Python, especially suitable for cleaning, processing and analyzing data. During the data analysis process, we often need to read data files in various formats, such as Txt files. However, some problems will be encountered during the specific operation. This article will introduce answers to common questions about reading txt files with pandas and provide corresponding code examples. Question 1: How to read txt file? txt files can be read using the read_csv() function of pandas. This is because

Practical Tips: How to use the trim function in PHP to process Chinese spaces Practical Tips: How to use the trim function in PHP to process Chinese spaces Mar 27, 2024 am 11:27 AM

In PHP programming, spaces are often encountered when processing strings, including Chinese spaces. In actual development, we often use the trim function to remove spaces at both ends of a string, but the processing of Chinese spaces is relatively complicated. This article will introduce how to use the trim function in PHP to process Chinese spaces and provide specific code examples. First, let us understand the types of Chinese spaces. In Chinese, spaces include not only common English spaces (space), but also some other special spaces.

How to use fopen, fwrite and fclose in php for file operations? How to use fopen, fwrite and fclose in php for file operations? Jun 01, 2023 am 08:46 AM

In PHP development, file operations are very common. Under normal circumstances, we need to perform file reading, writing, deletion and other operations. Among them, the fopen function and fread function can be used to read the file, and the fopen function, fwrite function and fclose function can be used to write the file. This article will introduce how PHP uses fopen, fwrite and fclose to perform file operations. 1. fopen function The fopen function is used to open files. Its syntax is as follows: r

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

What is the function of trim function of solid state drive? What is the function of trim function of solid state drive? Nov 21, 2022 am 10:58 AM

The trim function of the solid-state drive is mainly to optimize the solid-state drive, solve the problem of slowdown and lifespan of the SSD after use, and improve the efficiency of the SSD by preparing data blocks for reuse. The Trim function is a function that almost all SSD solid state drives have. It is an ATA command. When the system confirms that the SSD supports Trim and deletes data, it does not notify the hard disk of the deletion command and only uses the Volume Bitmap to remember that the data here has been deleted. This enables faster data processing.

See all articles