Scanning files and extracting specific information
Posted: Fri Jan 20, 2012 8:57 pm
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.
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.