Jump to content

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


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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.