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}