PDA

View Full Version : Split / explode a variable containing a command - possible?


cfructose
02-20-2007, 02:23 AM
I'm stumped on 'split' and 'explode'...

Here's what I'm doing: first, I'm putting an include of a data file into a template, but also establishing the varibale $add_content for later use:

$include_name = basename($_SERVER['PHP_SELF']);
$add_content = include ("text/$include_name");
echo $add_content;

So far so good. But now, I want to count how many instances of "</h2>" are in the data of the included file, and then perform an action if the number's less than 4:

$headers = split($add_content, "</h2>");
$number_of_headers = count($headers);
if ($number_of_headers>4) { $long=1; }
if (!($page == "About") || isset($long)) { do something }

I'm guesing my problem is that the array $headers doesn't contain any data from the file "text/basename($_SERVER['PHP_SELF'])", as, when I echo it as a test, it returns "1".
$number_of_headers also returns "1", regardless.

I get the same results using "explode" instead of "split".

If I've understood the concept, then $headers should be an array where [0] = all the data in the file "text/basename($_SERVER['PHP_SELF'])" until the first instance of "</h2>", and [1] contains all the data between the first and second "</h2>"s etc.

...Or have I completely lost the plot...? :-)

Can split/explode actually be used on a variable containing the comand to include a file? If not, is there a way?

Also, how do we explain the returned value of 1 when echoing $headers?

Any guidance very much appreciated.

Fou-Lu
02-20-2007, 06:39 AM
I'm tired and kinda skimmed the end.
But, you are looking the wrong way. An included file imports the data from another file into your current file. Assuming that your file has embedded html in it, it will look at the value, but not store it in the variable.
You are getting a 1 for your value because include is returning a boolean value. In order to capture the output you require the use of a reading (http://ca3.php.net/manual/en/ref.filesystem.php) method, such as fread. This also extracts all php data as well as the html data. You should be able to avoid that by using a eval. After that, you can look at the html and an explode will work fine.
As a side note, I've never tried it before but I'm wondering if you can capture an included file thats been processed into an output buffer. Anyone tried this method before?

cfructose
02-20-2007, 02:06 PM
Thanks for clearing that up. I presumed I was looking the wrong way!
OK - now I've got my work cut out...
:-)