[PHP Help] File Sections

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hey DevBest.

So I currently have a function that get's the contents of a file and saves it as a variable.
What I'm trying to do, is... let's say the contents of the file was:

Code:
<!DOCTYPE html>
<html>
<head>
[Content Inbetween]
</head>
<body>
[Content Inbetween]
</body>
</html>

I want to split the page into different sections - for different variables.
So I want to be able to have $head = "<!DOCTYPE html><html><head>[Content Inbetween]</head>"; and then $body = "<body>[Content Inbetween]</body></html>";

How would I achieve this?
Cheers.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Don't exactly know what you mean, but you could do:

PHP:
<?php
$head = '<title>Hello world</title>';
$body = '<p>Hi</p>';
?>
<!DOCTYPE html>
<html>
<head>
    <?php echo $head; ?>
</head>
<body>
    <?php echo $body; ?>
</body>
</html>
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Sorry, I'll try and put it a bit better.

So right now I have this:
PHP:
$dir = "tpl/";
$file = $this->cur_file;
$ext = ".php";
$con_file = $dir . $file . $ext;
if(file_exists($con_file))
{
        $this->file_html = file_get_contents($con_file);
        echo $this->file_html;
}
So $this->cur_file; is equal to whatever $_GET['cur_file']; is set to.
So if $_GET['cur_file'] = "index"; it would bring index.php from /tpl/ and set $this->file_html; equal to the contents of that file.

I then want to be able to separate the head from the body while keeping the current file contents so later on I can then append them to each other.
Get what I mean?
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I tried it out.

Code:
PHP:
$html = $this->file_html;
preg_match_all('/<head>(.*?)<\/head>/s', $html, $matches);
$this->file_html = $matches;

This was the result:
PHP:
Array ( [0] => )
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I tried that one directly after the first one didn't work, couldn't seem to get that one working either.
 

Users who are viewing this thread

Top