Page 1 of 1

[SOLVED] Grab content between <tags> in text file..

Posted: Thu Mar 11, 2004 1:01 am
by idotcom
Hi guys..

I'm seriously going nuts here!!

I need to grab all content between 2 tags in a text file on my server.

example: I need all of this
START....
{
I'm just using the forum PHP feature to make it easy to see certain stuff in here

Code: Select all

<html>
<head>
<title>Title of page</title>
<META NAME='description' CONTENT='Page Desc'>
<META NAME='keywords' CONTENT='keywords,keywords'>
<link rel='stylesheet' href='style.css' type='text/css'>
<script type='text/javascript' language='JavaScript1.2' src='reload.js'></script>
</head>
<body bgcolor='#FFFFFF' text='#000000'>
}
END....


out of the text file/page into variable(s) so that I can write them back to the file.

Why do I need to do this???

I am working on a template system that uses a MSHTML WYSIWYG editor and because of the MSHTML crap, I can only get everything BETWEEN the <body> Only this source </body> tags into the WYSIWYG edit field.

So when the page editing is done, and you click save, it only writes

Code: Select all

<table>
  <tr>
    <td> whatever is in the body of the page </td>
  </tr>
</table>
back to the file/page. This is no good... I need the head of the document back into the file for the meta tags and style and so on.... Thats why I need to grab the head of the document to be able to write it back.

When I was using just a textarea field it was fine.. But for someone that does not know html, that would be impossible to edit a page looking at a bunch of code.

I use this to OPEN FILE

Code: Select all

$file_content_read = implode("",file("$dir/$fne"));
$file_content = stripslashes($file_content_read);
then just echo/print the $file_content in the WYSIWYG field.

Once the changes are made and save is clicked WRITE TO FILE

Code: Select all

if($commit_edit)
{
$content = stripslashes($file_content);
$fp = fopen("$dir/$fn","w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
}
Any help would be GREATLY APPRECIATED!!!

an actual sample code to do this would be EXCELLENT!

Thank you in advance

Chris

PS I'm on a time crunch here.. so any ideas would do..

Re: Grab content between <tags> in text file.. Please

Posted: Thu Mar 11, 2004 8:03 am
by TheBentinel.com
idotcom wrote:I need to grab all content between 2 tags in a text file on my server.
Just to get the ball rolling, I'll take a stab at this. I think I misunderstand you, but at least the conversation is moving.

There is a text file that you need to open and grab the contents of the <head></head> tags, right?

So, could you do something like:

Code: Select all

$html = [read the file into this variable]
$startpos = strpos("<head>", $html);
$endpos = strpos("</head>", $html);
$headtext = substr($html, $startpos + 6, $endpos);
// $headtext now contains everything between the head tags
The +6 logic may not be dead on, but you can tweak both ends to make it work. This is the general idea.

If I understand your needs. Which I doubt I do. :-(

Hope it helped!

Posted: Fri Mar 12, 2004 3:27 am
by idotcom
Dave,

Thank you for your reply!

Based on your example I was able to put this together..

Code: Select all

function stripfromtext($haystack, $bfstarttext, $endsection) {
  $startpostext = $bfstarttext;
  $startposlen = strlen($startpostext);
  $startpos = strpos($haystack, $startpostext);
  $endpostext = $endsection;
  $endposlen = strlen($endpostext);
  $endpos = strpos($haystack, $endpostext, $startpos);
  return substr($haystack, $startpos + $startposlen, $endpos - ($startpos + $startposlen));
}
$filename = implode("",file("$dir/$file"));
$file_head = stripfromtext ($filename, "<html>", "<body bgcolor='#FFFFFF' text='#000000'>");
$current_header = "
<html>
$file_head
<body bgcolor='#FFFFFF' text='#000000'>
";
I can now copy all I need from the file/page which is easily specified in the $file_head.

<html> is the starting point and <body bgcolor='#FFFFFF' text='#000000'> end point.

Then I can rewrite the content thats between those two, back to the file on save.

Once again, Thank you. :D

Chris

BTW.. you probably know what all that code means.. I just tried to describe it for others that dont get it right off the bat.