martabau Posted December 23, 2008 Share Posted December 23, 2008 i have these 2 functions: Reading a Particular Line in a File $line_counter = 0; $desired_line = 29; $fh = fopen('vacation-hotspots.txt','r') or die($php_errormsg); while ((! feof($fh)) && ($line_counter <= $desired_line)) { if ($s = fgets($fh,1048576)) { $line_counter++; } } fclose($fh) or die($php_errormsg); print $s; AND: Modifying a File in Place Without a Temporary File: $fh = fopen('message.txt','r+') or die($php_errormsg); // figure out how many bytes to read $bytes_to_read = filesize('message.txt'); // initialize variables that hold file positions $next_read = $last_write = 0; // keep going while there are still bytes to read while ($next_read < $bytes_to_read) { /* move to the position of the next read, read a line, and save * the position of the next read */ fseek($fh,$next_read); $s = fgets($fh,1048576) or die($php_errormsg); $next_read = ftell($fh); // convert <b>word</b> to *word* $s = preg_replace('@<b[^>]*>(.*?)</b>@i','*$1*',$s); // convert <i>word</i> to /word/ $s = preg_replace('@<i[^>]*>(.*?)</i>@i','/$1/',$s); /* move to the position where the last write ended, write the * converted line, and save the position for the next write */ fseek($fh,$last_write); if (-1 == fwrite($fh,$s)) { die($php_errormsg); } $last_write = ftell($fh); } // truncate the file length to what we've already written ftruncate($fh,$last_write) or die($php_errormsg); // close the file fclose($fh) or die($php_errormsg); both work well to read a specific line of the file and the other to replace a part of the file or all the file, could some one tell me a function to combine that i mean a function to replace part of a line or all the line of an opened file but just in the line that i want, thanks a lot Link to comment https://forums.phpfreaks.com/topic/138230-modify-file-with-php/ Share on other sites More sharing options...
Brian W Posted December 23, 2008 Share Posted December 23, 2008 no one has replied cuz you need to put your code in [ code ] [ /code ] tags (without the spaces inside the []). It makes reading the code easier. Link to comment https://forums.phpfreaks.com/topic/138230-modify-file-with-php/#findComment-722730 Share on other sites More sharing options...
pinacoladaxb Posted December 23, 2008 Share Posted December 23, 2008 agree Link to comment https://forums.phpfreaks.com/topic/138230-modify-file-with-php/#findComment-722737 Share on other sites More sharing options...
Brian W Posted December 23, 2008 Share Posted December 23, 2008 about 13 people have looked at this post and not actually replied with help while other posts all around it with more difficult and even less exciting questions get answered. I'll be honest, I haven't even read the first line of this guys code yet... lol Link to comment https://forums.phpfreaks.com/topic/138230-modify-file-with-php/#findComment-722741 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.