import sys with open('index.php', 'r') as f: lines = f.readlines() start_marker = "" in line and i > 1200: # around there else_line = i break if not else_line: print("Error: Could not find the final else line") sys.exit(1) # Now find the first foreach after this else start_line = 0 for i in range(else_line, len(lines)): if start_marker in lines[i]: start_line = i break if not start_line: print("Error: Could not find start marker after else") sys.exit(1) # Now find the end of this block. It should end with # But I might have multiple ones now. # We want to find the one that is followed by (closing the main if) # and then (closing chat-container or messages-list) end_line = 0 for i in range(start_line, len(lines)): if "" in lines[i]: # Check if next line (or soon after) is is_real_end = False for j in range(i+1, min(i+10, len(lines))): if "" in lines[j]: is_real_end = True end_line = j break if is_real_end: break if not end_line: print("Error: Could not find end marker") sys.exit(1) print(f"Replacing lines {start_line+1} to {end_line+1}") new_block = """
""" # Adjust end_line to be the last endif before the start of next section # The original file has at line 1486 in my previous read. # Let's verify. with open('index.php', 'w') as f: f.writelines(lines[:start_line]) f.write(new_block) f.write("\n") f.writelines(lines[end_line+1:])