Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Extracting a variable froma php file

How to?



7:40 pm on Aug 1, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Hi all

I have a bunch of files in a directory and each file (at some point) will have a line like this:

$title="My page title";

I need to extract the value of $title from each file in the directory to make a dynamic menu.

Problem is that I break out in a cold sweat just thinking about regexp, is their a simple way to do this?




9:02 pm on Aug 1, 2002 (gmt 0)

10+ Year Member

I can't think of a simpler way right now but you could try this:

loop through your directory and for each file you can grab the title with this (can't test the ereg right now so let's it's quite possible it's not functional yet):

if (ereg("\$title=\"([^\"]+)\";", $filecontent, $regs) {
$title = $regs[1];
} else {
$title = "";

<added>On a second thought, you could always make sure that the actual line is the first line of every file and you can simply read the file grab the first line, grab the $title content with a simple substring trick</added>



11:42 pm on Aug 1, 2002 (gmt 0)

10+ Year Member

Wait a sec.
If I understand (correct me if I'm wrong), but you DO have $title="whatever" in your files?
Because if that't the case, then you could loop, perhaps something like this:

$dir = opendir("/path/to/files/");
while (($file != readdir($dir))!==false) {
if ($file!="." && $file != "..") {
if (is_file("/path/to/files/".$file) {
include ("/path/to/files/".$file);
$allTitles[] = $title;

Or something like that, I didn't test it... :)

If you need to extract the title from the title tag, well I guess the previous loop will work but you'll need to change the include line for, probably, a regex expression.

Hope this helps, good luck!

Thors Hammer

11:55 pm on Aug 1, 2002 (gmt 0)

10+ Year Member

Here is a resource I ran across, that I dont know if it will contain what you are looking for, but it looks immense. :)

Hope this helps...




Featured Threads

Hot Threads This Week

Hot Threads This Month