PHP file operation: Insert a row, delete a row, and obtain the row number.

Source: Internet
Author: User
1 # insert content in a new row after the content to be searched 2 function insertaftertarget ($ filepath, $ insertcont, $ target) 3 {4 $ result = NULL; 5 $ filecont = file_get_contents ($ filepath); 6 $ targetindex = strpos ($ filecont, $ target); # search for the coordinates of the target string 7 8 if ($ targetindex! = False) {9 # Find the last line break of Target 10 $ chlineindex = strpos (substr ($ filecont, $ targetindex), "\ n") + $ targetindex; 11 if ($ chlineindex! = False) {12 # Insert the content to be inserted 13 $ result = substr ($ filecont, 0, $ chlineindex + 1 ). $ insertcont. "\ n ". substr ($ filecont, $ chlineindex + 1); 14 $ fp = fopen ($ filepath, "W +"); 15 fwrite ($ FP, $ result ); 16 fclose ($ FP); 17} 18} 19} 20 21 # delete a row of 22 function deltargetline ($ filepath, $ target) 23 {24 $ result = NULL; 25 $ filecont = file_get_contents ($ filepath); 26 $ targetindex = strpos ($ filecont, $ target); # Find the target string Coordinates 27 28 if ($ targetindex! = False) {29 # Find the first line break of target 30 $ prechlineindex = strrpos (substr ($ filecont, 0, $ targetindex + 1), "\ n "); 31 # Find the last line break of target 32 $ afterchlineindex = strpos (substr ($ filecont, $ targetindex), "\ n") + $ targetindex; 33 if ($ prechlineindex! = False & $ afterchlineindex! = False) {34 # re-write and delete the content of the specified row 35 $ result = substr ($ filecont, 0, $ prechlineindex + 1 ). substr ($ filecont, $ afterchlineindex + 1); 36 $ fp = fopen ($ filepath, "W +"); 37 fwrite ($ FP, $ result ); 38 fclose ($ FP ); 39} 40} 41} 42 43 # obtain the row number of a segment. 44/** 45 * @ Param $ filepath46 * @ Param $ target: Field 47 * @ Param bool $ first after matching the first field, exit 48 * @ return array49 */50 function getlinenum ($ filepath, $ target, $ first = false) 51 {5 2 $ fp = fopen ($ filepath, "R"); 53 $ linenumarr = array (); 54 $ linenum = 0; 55 while (! Feof ($ FP) {56 $ linenum ++; 57 $ linecont = fgets ($ FP); 58 If (strstr ($ linecont, $ target )) {59 If ($ first) {60 return $ linenum; 61} else {62 $ linenumarr [] = $ linenum; 63} 64} 65} 66 return $ linenumarr; 67}

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.