homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Extracting a variable froma php file
How to?
Nick_W




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

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?

Cheers

Nick

 

mavherick




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

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>

mavherick

transistor




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

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




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

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...

[web.lconn.com...]

Thor

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved