phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP File: Removing Lines From File

Tag: file, remove line   Category: PHP Basic
post: 14 Apr 2008 read: 1,077


PHP File Tips - Part 13: If we want to remove line from file, we can use this trick. Read file into array, then unset item that we want remove. Last, write back to file.

<?php
$f = "test.txt";

// read into array
$arr = file($f);

// remove second line
unset($arr[1]);

// reindex array
$arr = array_values($arr);

// write back to file
file_put_contents($f,implode($arr));
?>


Series this article:
PHP File: 3 Way of Reading File
PHP File: Reading File at Particular Lines
PHP File: Reading File at Particular Byte Ranges
PHP File: Checking File or Directory
PHP File: Showing All File in A Directory
PHP File: Showing All Files and Folders in Particular Directory
PHP File: Retrieving File Statistic
PHP File: Retrieving More File Information
PHP File: Counting Lines in a File
PHP File: Counting Characters and Words in File
PHP File: Writing File
PHP File: Locking and Unlocking File
PHP File: Removing Lines From File
PHP File: Inserting Lines into a File

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting