Scanning files and extracting specific information

Ask about a PHP problem here.
Post Reply
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Scanning files and extracting specific information

Post by kgdd »

Thanks in advance for any help, I am new to this still and need some assistance.

I am attempting to write a function that continuously scans a given directory looking for php files. Upon finding those files, the function will then scan each file looking for a "tagged area" such as {grabthisarea->('text')}, and pull out the word text and create a column in a table on my db with the name of text if it doesn't already exist.

Here is what I have so far, and it's not really working. (I'm afraid to with having it run continuously that will eat away at server speed)


[syntax=php]<?
function grabcontentareas() {

$result = mysql_query("SELECT COLUMN_NAMES FROM pages ");
$row = mysql_fetch_array($result);
$exists = $row['column_name'];

// Scan directory for files
$dir = "(directory path)";
$files = glob($dir."/*.php");

// Iterate through the list of files
foreach($files as $file)
{
// Determine info about the file
$parts = pathinfo($file);

do
{
// If the file extension == php
if ( $parts['extension'] === "php" )
{
// Read the contents of the file
$contents = file_get_contents($file);

// Find first occurrence of opening content area tag
$from = strpos($contents, " {grabthisarea-->(' ");

// Find first occurrence of ending content area tag
$to = strpos($contents," ' ) } ");

// Pull out the unique name from between the content area tags
$contentarea = substr($contents, $from, $to);

if ( $exists !== $contentarea)
{

( " ALTER TABLE pages ADD '$contentarea' LONGTEXT NOT NULL " );

}
else ''; // not sure what to put here.

}
}

while ($contentarea !== $exists); // does the loop continuously i think..

}

}
?>[/syntax]

Sorry if I am being confusing. I have tried testing and it doesn't seem to be working quite right. Please let me know if I am headed in the right direction or if something needs to be changed up.

As a Note: each php file might have multiple {grabthisarea->'s in it.
Last edited by jacek on Sun Jan 22, 2012 2:35 pm, edited 1 time in total.
Reason: code tags...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Scanning files and extracting specific information

Post by jacek »

kgdd wrote: (I'm afraid to with having it run continuously that will eat away at server speed)

It will ! Even worse, constantly read the file will prevent other things (like the webserver) reading it.

I don't really see how this method will ever work, so it might be a better to think of some alternative methods. What are you actually trying to do here ?
Image
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Re: Scanning files and extracting specific information

Post by kgdd »

Scan PHP files in a single directory, explode each file, look at it's contents and search for {grabthisarea->('TEXT')} and pull out the word TEXT. (there can be multiple grabthisarea's on each page) Then search the table to see if the column name already exists or not. If it doesn't exist then ALTER the table and add the column name, if it does exist don't add it and continue on to the next instance. I will set it up so that it only runs once when a certain page (home page) is visited. I am building a custom CMS system and am setting up an edit page that when you click on the php file name in a drop down menu, it pulls in the column names that have content relevant to that file name which is stored in the table, and then display the information in those fields.

Make sense..? Or no? Sorry for the confusion!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Scanning files and extracting specific information

Post by jacek »

Right, I don't see why you need a loop then :s You don't want to constantly scan the file for changes you just want to get the matches once.

Regular expressions can help here.

[syntax=php]$file = file_get_contents('example.php');

preg_match_all('#{grabthisarea\->\('([a-z]+)'\)}#ui', $matches);[/syntax]
Something like that anyway. Then try printing $matches

[syntax=php]print_r($matches);[/syntax]
to see what this gives you.
Image
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Re: Scanning files and extracting specific information

Post by kgdd »

I tried this based off of your code:

[syntax=php]<?
$file = file_get_contents('templates/test.php');

preg_match_all("{(grabthisarea>>'([a-z]+)')}"," ",$matches);

print_r($matches);

?>[/syntax]

Got this in the browser:

Array ( [0] => Array ( ) [1] => Array ( ) [2] => Array ( ) )

Here is what the php file that I'm scanning looks like:

[syntax=php]<?
/*
{{{{asdf}}}}
*/
?>


<strong>asdf</strong>

<?

/*

{(grabthisarea>>'test1')}

*/

?>

<strong>asdf</strong>

<?

/*

{(grabthisarea>>'test2')}

*/

?>[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Scanning files and extracting specific information

Post by jacek »

[syntax=php]preg_match_all("{(grabthisarea>>'([a-z]+)')}"," ",$matches);[/syntax]
The second parameter here should be the text to be search so

[syntax=php]preg_match_all("{(grabthisarea>>'([a-z]+)')}", $file, $matches);[/syntax]
and ( need sto be escaped in the expression so

[syntax=php]preg_match_all("{\(grabthisarea>>'([a-z]+)'\)}", $file, $matches);[/syntax]

DISCLAIMER: I didn't test this.
Image
kgdd
Posts: 17
Joined: Fri Nov 04, 2011 2:24 pm

Re: Scanning files and extracting specific information

Post by kgdd »

Hey thanks for the help but I actually got it all to work!
Post Reply