• 0

PHP include?


Question

I know that "include" would insert an entire page, but is there any way to take a small portion of that page, something that has already defined as

<something>

Blah Blah, Some odd text.

<something>

Not sure if I'm makeing sence, but that's why i'm asking. :blush: Thanks!

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

You could have the included file being a list of functions, with each function printing out a small portion. At the top of your script, require_once("page.inc.php"); and then when you want to print a section, call the relevent function. This isn't a great way of doing it, but it'll work. Only problem I can think of could be with variables' being global or not.

Another solution would be to have several includes and use a long if statement or a switch to choose the right one.

A third solution and possibly the best would be to set a variable before calling the include. Then in the include, have an if statement or switch or whatever fits which checks the variable and only prints what's needed.

Link to comment
Share on other sites

  • 0

Well I tried a little bit of everything but none seemed to work so let me rephrase.

Basically all I want to do is take a chunk of text from one page and include it in another. The text is encapsulated in <body_txt> </body_txt>

I don't need the entire page, just the text from this area, as the text changes every so often, but the name body_txt doesn't. I don?t have much PHP coding under my belt, thus the tread, but I figured PHP would be ideal for this job, but maybe not.

Any other suggestions would be appreciated!

Link to comment
Share on other sites

  • 0

I know there's a way to do that, but no clue what it is. So my best guess is this-

fwrite() the body text to bodytext.php

have both pages <? include(bodytext.php) ?>

Link to comment
Share on other sites

  • 0

I like that suggestion, but my next question would be: how do I get the "body txt" into a string. It prob has to do with pointers, but I can't seem to figure it out. Thanks again!

Link to comment
Share on other sites

  • 0

you could use regular expressions (ereg, preg_match)

ie (where <larf> is a unique section of text before the part you want to match, and </larf> is a unique section after the part you want to match)

$file = fopen("file.html", "r+");

$contents = fread($file, filesize("file.html"));

preg_match("/<larf>(.*)</larf>/", $contents, $matches);

echo($matches[0]);

i'm not sure if that's exactly syntaxically correct but it's a step in the right direction

the only problem with this method is having to load the entire file into ram first

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.