Php implements the method of inserting data in a specified row in a file

Source: Internet
Author: User
Welcome to the Linux community forum, and interact with 2 million technical staff to access the php Method for inserting data in the specified row of the file. It is complicated to use php to insert data in the specified row of the file, check the following code? Php $ arrInsertinsertContent ("array. php", "abcdef", 3, 10); unlink ("array. php ")

Welcome to the Linux community forum and interact with 2 million technical staff> enter the php implementation file to specify the method for inserting data in the row, using php to insert data in the specified row of the file is complicated. Check the following code? Php $ arrInsert = insertContent ("array. php", "abcdef", 3, 10); unlink ("array. php ")

Welcome to the Linux community forum and interact with 2 million technicians>

Php implements the method of inserting data in specified row of the file. It is complicated to use php to insert data in specified row of the file. See the following code.

$ 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 multi-data storage, data is stored in the database tutorial. In the above example, the data is stored in the text in the X format. Now I want to operate like a database, the row that you want to delete is the same as the row that stores the data. How to read the row that contains the row number is the same, so I wrote php to insert the data instance in the specified row of the file.

?>

$ ILine: the number of lines, $ index before the number of characters

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.