PHP inserts a data instance into a file specified line _php tutorial

Source: Internet
Author: User
PHP tutorial Inserting a data instance on a file-specified line
For PHP file operations so it is more complicated to insert data at a specified location, let's look at the relationship Insert data instance in the specified row of the file.


$arrInsert = Insertcontent ("array.php", "abcdef", 3, 10);
Unlink ("array.php");
foreach ($arrInsert as $value)
{
File_put_contents ("array.php", $value, File_append);
}


function Insertcontent ($source, $s, $iLine, $index) {
$file _handle = fopen ($source, "R");
$i = 0;
$arr = Array ();
while (!feof ($file _handle)) {

$line = fgets ($file _handle);
+ + $i;
if ($i = = $iLine) {
if ($index = = strlen ($line)-1)
$arr [] = substr ($line, 0, strlen ($line)-1). $s. "N";
Else
$arr [] = substr ($line, 0, $index). $s. substr ($line, $index);
}else {

$arr [] = $line;
}
}
Fclose ($file _handle);
return $arr;
}
In the majority of our data is stored in the database tutorial, we have the data in the X format in the text, and now I want to do the same as the database, I want to delete the row that row, save the data also, how to read the first few lines on the first line, so I wrote the PHP Inserts a data instance into the specified row of a file.
?>
$iLine: For the first few lines, $index before the first character


http://www.bkjia.com/PHPjc/444951.html www.bkjia.com true http://www.bkjia.com/PHPjc/444951.html techarticle PHP Tutorial Inserts a data instance into a file specified line for PHP file operations so about inserting data at a specified location is more complicated, and let's look at the relationship in the file specifying the line interpolation ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.