monotoko Posted May 1, 2010 Share Posted May 1, 2010 Hiya Guys Could just do with a bit of help with some good ol' file manipulation, what i need to do is grab the source of a page (which i have done) and it is now in an array (it could be in a string if it would be easier?) I now need to replace the part of the file in between <head> and </head> with a new set of lines, deleting the old ones. Does anyone know how i would go about such a plan? I cant see any existing functions to do it...i could create one if you guys would just point me in the right direction of which PHP fuctions to use? Quote Link to comment https://forums.phpfreaks.com/topic/200370-file-manipulation/ Share on other sites More sharing options...
siric Posted May 1, 2010 Share Posted May 1, 2010 A string would be easier for this method. You need to find the position of <head and </head>. You then replace all text between first and last string positions. <? $text = "<head>This is a sentence one. This is sentence two.</head>"; $headstart = stripos($text, "<head>"); $headend = stripos($text, "</head>"); $textstart = $headstart + 6; //length of <head> is added $textend = $headend; $textlength = $textend - $textstart; $replacement = "This is the replacement text"; $newtext = substr_replace($text,$replacement,$textstart,$textlength); print "newtext - $newtext"; ?> Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/200370-file-manipulation/#findComment-1051530 Share on other sites More sharing options...
Axeia Posted May 1, 2010 Share Posted May 1, 2010 Like javascript, PHP is also able to manipulate a page via the DOM. So there is existing functionality to do the job it does however require well-formed HTML. The documentation page of the removeChild method also has an example of how to load a file. (for HTML use $doc->loadHTML() instead of $doc->load() - XHTML should be loaded as XML) Quote Link to comment https://forums.phpfreaks.com/topic/200370-file-manipulation/#findComment-1051539 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.