k21chrono Posted October 12, 2009 Share Posted October 12, 2009 I am wondering this because i am trying to write to a portion in the middle of the document not the beginning or the end, any suggestions are fine TY for the help ahead of time. Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/ Share on other sites More sharing options...
pastcow Posted October 12, 2009 Share Posted October 12, 2009 You could use filesize("filename") to get the size of the file, then fseek to the middle of the file (which would be filesize / 2) - your file pointer would then be in the middle. Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-935566 Share on other sites More sharing options...
k21chrono Posted October 12, 2009 Author Share Posted October 12, 2009 You could use filesize("filename") to get the size of the file, then fseek to the middle of the file (which would be filesize / 2) - your file pointer would then be in the middle. What if its not exactly in the middle and I'm looking more for the coding i would have to use and not the method to find the location, thanks though. Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-935579 Share on other sites More sharing options...
pastcow Posted October 12, 2009 Share Posted October 12, 2009 Are you searching for a specific term you want to replace somewhere near the middle? If so what are you searching for and how is the file structured (is it purely binary or is it text with new lines etc..) Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-935585 Share on other sites More sharing options...
k21chrono Posted October 14, 2009 Author Share Posted October 14, 2009 Are you searching for a specific term you want to replace somewhere near the middle? If so what are you searching for and how is the file structured (is it purely binary or is it text with new lines etc..) Ok say i have <html> <head> </head> <body> insert some dumb bullcrap here </body> </html> and now say i wanna put something into the document in the head section, how would i put my file pointer inside the head tag tyvm in advance. Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-936845 Share on other sites More sharing options...
k21chrono Posted October 14, 2009 Author Share Posted October 14, 2009 Bump Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-936860 Share on other sites More sharing options...
knsito Posted October 14, 2009 Share Posted October 14, 2009 Dont think this can be done with fseek. Probably would need to store your file in a variable, make your changes, and write it back (?) If your head section is empty or you know exactly what is you want to replace, you can do a str_replace which would be fine. If its more complicated you probably should use regex replace functions Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-936865 Share on other sites More sharing options...
k21chrono Posted October 16, 2009 Author Share Posted October 16, 2009 Dont think this can be done with fseek. Probably would need to store your file in a variable, make your changes, and write it back (?) If your head section is empty or you know exactly what is you want to replace, you can do a str_replace which would be fine. If its more complicated you probably should use regex replace functions i dont exactly want to replace something, just add more to a specific location in the document. <html> <body> <div id="list" </div> </body> </html> and say i want to make a form and php that adds things into the list div tag, if that helps clarify it a little Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-938147 Share on other sites More sharing options...
mikesta707 Posted October 16, 2009 Share Posted October 16, 2009 try using preg_replace Im not very good with regex, but here is an example $pattern = '#\<head\>(.+?)\<\/head\>#s'; $replace = "content to replace" $file = get_file_contents("myfile.html"); preg_replace($pattern, $replace, $file); //then write to the file down here that pattern may be slightly off, but you get the idea Quote Link to comment https://forums.phpfreaks.com/topic/177445-solved-how-would-you-place-a-file-pointer-at-a-specific-spot-in-a-document/#findComment-938150 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.