$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/categories/'.$_GET['category'].'/'.$_GET['subcategory'].'/data.txt', 'w'); if ($fp === false) die("Couldn't open data.txt for writing!"); foreach ($domains as $dom => $count) { [color=red]if(eregi($dom,$_SERVER['HTTP_HOST'])) { //I WANT TO REMOVE THE LINE } else {[/color] fwrite($fp, "$dom $count\n"); [color=red]}[/color] } fclose($fp); Code (markup): In the above code, I want to remove a line (within data.txt) if it contains the current domain name. I have added the parts in red. What am I doing wrong?
I already have functions to delete and recreate the file. However, when the file is being created, I want to detect if certain lines contain HTTP_HOST and remove those lines.
I'm not sure I completely understand what you're trying to do, it sounds like what you want can be accomplished by using file_get_contents to read the file into a string, using eregi_replace, and then writing the string back into the file.
never mind I had someone help me.. I was on the right track. To do what I wanted the code is: if(eregi($_SERVER['HTTP_HOST'], $dom)) { echo ""; } else { fwrite($fp, "$dom $count\n"); } Code (markup):
if (!eregi($_SERVER['HTTP_HOST'], $dom)) fwrite($fp, "$dom $count\n"); PHP: Should accomplish the same, without the unecessary echo.
Do not use regular expressions for these 'small' tasks. Use str_replace() instead. Regex are more resource consuming and powerful, both of which are not needed for your case. Also, if you insist on regex, then use preg_* functions instead of ereg() functions. Ereg functions have some bugs/flaws.