Solved

Using the glob function

Posted on 2008-06-26
7
271 Views
Last Modified: 2008-06-28
Hi guys...

One of you kind gentlemen helped me with a glob function which works great.
Id like to just modify it slightly.
Here is a quick code snippet..

foreach ( glob('*.php') as $filename ) {

In the above, this finds all php files in a folder.
Id like to just find php files that start with the following....

menu
followed by
a number (1 digit long)
then any character/s
followed by
.php

So, for example,

menu1_it_shhjksdfljldjsf.php    (it would find this)
backup.menu3_it_sdfs.php    (it would NOT find this)
menu_templates.php         (it would NOT find this because it doesnt have a digit after menu)


So, i just need

foreach ( glob('*.php') as $filename ) {

modified to be something like

foreach ( glob ('^menu<a digit number>*.php') as $filename )

Ive tried the above but it doesnt work.

Any help greatly appreciated.

0
Comment
Question by:Simon336697
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
JurgenvH earned 300 total points
ID: 21878207
Something with regular expressions should work:
$pattern = "/\(^menu[0-9]\)\([a-zA-Z0-9]\)/";
 
foreach ( glob('*.php') as $filename ) {
 
	if (preg_match($pattern,$filename) == 1) {
	
		//do what you like with the file
 
	}
 
}

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21880240
Hi JurgenvH,
Im having issues Jurgen with embedding the above regEX into my exisiting code.
The below file is where i have embedded it.

 <?php
 
 // Make a MySQL Connection
 mysql_connect("localhost", "root", "password") or die(mysql_error());
 mysql_select_db("simeswiki") or die(mysql_error());
 
 
 // Examine the files on disk --> loop through them --> create records in filename_fil table.
 
 $pattern = "/\(^menu[0-9]\)\([a-zA-Z0-9]\)/";
 
 foreach ( glob('*.php') as $filename ) {
 
                 if (preg_match($pattern,$filename) == 1) {
 
                         $query =  "select count(*) from filename_fil where file_fil='$filename'";
 
                         if (false == ($r = mysql_query($query))){
                                 die("Query failed: $query.<br />MySQL said: " . mysql_error());
                         }
                 }
 
                         list ( $cnt ) = mysql_fetch_row($r);
 
                                 if ( $cnt ) {
                                 // file exists in the database, do nothing..
                                  }
 
                                 else {
                                 // file does not exist
 
                                         $query = "insert into filename_fil (file_fil) values ('$filename')";
 
                                         if (false === mysql_query($query)){
                                                 die("The query failed: $query.<br />MySQL said: " . mysql_error());
                                         }
                                 }
 }
 
 
 // Examine entries in the database --> while looking at each, delete the db record if it does not exist on disk.
 
 $query = "SELECT file_fil FROM filename_fil";
 if (false === ($results = mysql_query($query)))
 {
    die("Query failed: $query. MySQL said: " . mysql_error());
 }
 
 while (false !== ($row = mysql_fetch_object($results)))
 {
    if (!file_exists($row->file_fil))
    {
         $deleteQuery = "DELETE FROM filename_fil WHERE file_fil='$row->file_fil' LIMIT 1;";
         if (false === mysql_query($deleteQuery))
         {
              die("Query failed: $query.<br />MySQL said: " . mysql_error());
         }
     }
 }
?>

Open in new window

0
 
LVL 2

Expert Comment

by:JurgenvH
ID: 21881561
Sorry, I made it to complicated. :-)
I think this is what you want. Works for me!
<?php
 
// Make a MySQL Connection
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("simeswiki") or die(mysql_error());
 
 
// Examine the files on disk --> loop through them --> create records in filename_fil table.
 
foreach ( glob('*.php') as $filename ) {
 
	$pattern = "/^menu[0-9]/";
 
	if (preg_match($pattern,$filename) == 1) {
 
		$query =  "select count(*) from filename_fil where file_fil='$filename'";
 
		if (false == ($r = mysql_query($query))){
			die("Query failed: $query.<br />MySQL said: " . mysql_error());
		}
 
		$cnt = mysql_num_rows($r);
 
		if ( $cnt > 0) {
			// file exists in the database, do nothing..
		} else {
			// file does not exist
			$query = "insert into filename_fil (file_fil) values ('$filename')";
 
			if (false === mysql_query($query)){
				die("The query failed: $query.<br />MySQL said: " . mysql_error());
			}
		}
	}
 
}
 
 
// Examine entries in the database --> while looking at each, delete the db record if it does not exist on disk.
 
$query = "SELECT file_fil FROM filename_fil";
if (false === ($results = mysql_query($query))) {
	die("Query failed: $query. MySQL said: " . mysql_error());
}
 
while (false !== ($row = mysql_fetch_object($results)))
{
	echo "Examine ".$row->file_fil."<br/>";
	if (!file_exists($row->file_fil)) {
        $deleteQuery = "DELETE FROM filename_fil WHERE file_fil='$row->file_fil' LIMIT 1;";
        if (false === mysql_query($deleteQuery)) {
			die("Query failed: $query.<br />MySQL said: " . mysql_error());
        } else {
			echo "File does not exists; deleted.<br/>";
		}
    }
}
?>

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 9

Assisted Solution

by:tkalchev
tkalchev earned 200 total points
ID: 21881701
Hi again Simon336697 :) I think you haven't read the manual of the glob function very carefully. There stands, that you can give any shell pattern as parameter to it. So you just need to write :

glob ( "menu[0123456789]*.php" )
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21881922
Hi tkalchev,
Thanks so much again tkalchev.....I will test this out and report back to your kind self :>)
0
 
LVL 1

Author Comment

by:Simon336697
ID: 21890860
Hi tkalchev,

Its not working for me.
I have tried the following, and it is not doing as expected.
I only want entries that start with menu[0123456789]*.php to be inserted into the database.
Now no new entries with the above criteria are being inserted, in fact I dont think anything new is.
Also, nothing is being removed from the database if that file no longer exists on disk, which is what my previous code did do.

In the code snippet below is my original code that did the following..

1) Inserted all php files from the current folder into the database
2) Removed any entries from the database if no longer exist on disk

All i want to do now is just to limit the entries into the database by only inserting ones that start with menu[0123456789]*.php

Here is that code..


 <?php
 
 // Make a MySQL Connection
 mysql_connect("localhost", "root", "password") or die(mysql_error());
 mysql_select_db("simeswiki") or die(mysql_error());
 
 
 // Examine the files on disk --> loop through them --> create records in filename_fil table.
 
 foreach ( glob('*.php') as $filename ) {
         $query =  "select count(*) from filename_fil where file_fil='$filename'";
                 if (false == ($r = mysql_query($query)))
                 {
                 die("Query failed: $query.<br />MySQL said: " . mysql_error());
         }
   list ( $cnt ) = mysql_fetch_row($r);
   if ( $cnt ) {
       // file exists in the database
   }
   else {
      // file does not exist
 
 $query = "insert into filename_fil (file_fil) values ('$filename')";
 if (false === mysql_query($query))
 {
    die("The query failed: $query.<br />MySQL said: " . mysql_error());
 }
   }
 }
 
 
 // Examine entries in the database --> while looking at each, delete the db record if it does not exist on disk.
 
 $query = "SELECT file_fil FROM filename_fil";
 if (false === ($results = mysql_query($query)))
 {
    die("Query failed: $query. MySQL said: " . mysql_error());
 }
 
 while (false !== ($row = mysql_fetch_object($results)))
 {
    if (!file_exists($row->file_fil))
    {
         $deleteQuery = "DELETE FROM filename_fil WHERE file_fil='$row->file_fil' LIMIT 1;";
         if (false === mysql_query($deleteQuery))
         {
              die("Query failed: $query.<br />MySQL said: " . mysql_error());
         }
     }
 }
 
 ?>

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 21890926
Guys ill split points here and repost.
Thanks for your help.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question