PDA

View Full Version : Get PART of a file (via ID)



croatiankid
03-06-2007, 09:09 PM
So I want to get part of a file, specifically everything WITHIN the div id="left". I have this code, but it gets the whole file. I want to use this to edit a remote file (test.html).

<?
$myHTMLFile = "test.html";
$myHTML = implode( file( $myHTMLFile ), "" );

if ( $save ) {
$tmpfile = fopen( $myHTMLFile, "w" );
$fp = fwrite( $tmpfile, $myHTMLText );
fclose($tmpfile);
}
?>

<html>
<head>
<title>Update.php</title>
</head>
<body>
<div align=center>
<form action="update.php">
<textarea name="myHTMLText" rows=35 cols=70><? echo $myHTML ?></textarea>
<br>
<input type="submit" name="save" value="save">
</form>
</div>
</body>
</html> .
The above file will be for example Update.php

I just edited this, what was written here before is no longer needed, just the part about getting part of the file via ID.

Inigoesdr
03-06-2007, 10:09 PM
You would have to use preg_replace() (http://www.php.net/manual/en/function.preg-replace.php) or ereg_replace() to search the file to replace the text. Ex.:


$line = preg_replace('/<div id="left">(.*)<\/div>/', $newtext, $line);

Or preg_match() (http://www.php.net/manual/en/function.preg-match.php) to just get the text.

croatiankid
03-06-2007, 10:53 PM
So how would I use that in the above code? I didn't write the above code and don't really understand arrays (only basic PHP). I mean I see how I can get the text, but firstly I need to tell it to stop at the first </div> after, then i need to get that text in the text area, and THEN I need to replace it.

Another thing: would this be easier with just a simple .txt file that would be included in the div, and in effect this script would just edit the txt file? hmm yes I think I"ll do it that way?

aedrin
03-06-2007, 11:24 PM
would this be easier with just a simple .txt file that would be included in the div, and in effect this script would just edit the txt file? hmm yes I think I"ll do it that way?

Easier, and more reliable.

And arrays are your friend, practice using them. They're really simple yet powerful.