Jump to content

[SOLVED] How would you place a file pointer at a specific spot in a document?


k21chrono

Recommended Posts

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. :D

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 :D tyvm in advance.

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

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 :D

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.