Jump to content

file manipulation


monotoko

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.