Forum Moderators: coopster

Message Too Old, No Replies

Parsing strings

         

etias

8:50 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



I have a PHP code that lists things that are in my directory and I want it to ONLY show files that start with name-
How might I go about doing this? This is the php code I am using to list ALL files

<?

//define the path as relative
$path = "/hshome/etias/";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Directory Listing of $path<br/>";

//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "<a href='$file'>$file</a><br/>";
}

//closing the directory
closedir($dir_handle);

?>

jatar_k

9:57 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld etias,

you will need some extra logic in this bit

if($file!="." && $file!="..")
echo "<a href='$file'>$file</a><br/>";
}

what pattern do the filenames follow?

are they always 4 letters followed by a hyphen?

Robber

10:00 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



You could try changing your if test to the following:

if($file!="." && $file!=".." &&!preg_match("/^name-/",$file)){
echo "<a href='$file'>$file</a><br/>";
}

etias

10:43 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



Yes, the names will always be name-whatever.html and thank you for the quick response and hearty welcome. I just started PHP yesterday and so far i think I have learned a lot.

One question I have though...

Right now I have a couple forms in the html file that they fill out. The file is test.html. It then sends the info to post.php which parses it all and makes takes the questions and makes them into the name-namehere.html file. The namehere is the name of the person who took the test.

The admins can then login and see which files are there based on the names-whatever.html parsing and they can pick one and view the html file to see the answers and delete it.

Now that you know how it works. When the people take the test in the html file does it have to be in a separate file?

Can I take those forms that are in the html file and put them in a php file so it is all one or must it be an html that passes the arguments to the php?

I hope that made sense

etias

10:52 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



Robber, I tried that and it didn't change the listing at all, still shows everything

etias

11:30 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



Ah, found the problem, in your script you said:

if($file!="." && $file!=".." &&!preg_match("/^name-/",$file)){

The real code is
if($file!="." && $file!=".." &&preg_match("/^name-/",$file)){

You had it checking to make sure preg_match DID NOT match by adding the!.

Thanks for the code, it works wonders

etias

11:36 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



ok, one more thing....you all must think I love to talk. With the code

while ($file = readdir($dir_handle))
{
if($file!="." && $file!=".." &&preg_match("/^name-/",$file)){
echo "<a href='$file'>$file</a><br/>";
}
}

How would I make it that for the echo it would do the full a href like it is but for the display part of the link it would only be what is AFTER name-?

So if the file was name-bob.html it would make that the link and just display bob

jatar_k

11:56 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, maybe someone can throw a 'grab everything from the hyphen to the dash' pattern

>> When the people take the test in the html file does it have to be in a separate file?

no it could be appended to one master file. You could use a comma delimited format or maybe pipe delimited depending on what kind of input there is. This would be a flat file database.

I have a few questions

what happens when 2 people with the same name take the test, does it overwrite the file?

why not, as you asked, use a single file, or even better a database?

why the strange naming convention for the files, why not just their full name .html?

etias

12:05 am on Feb 16, 2006 (gmt 0)

10+ Year Member



This is for a video game site where the users register by their character name. There can be no two characters that are the same so there will be no problem with having the files over write each other.

As for using a database or a single file...I just started PHP yesterday by teaching myself and I thought that if you have a submit form it had to send it to an external file.

Such as:

<form method="post" action="post.php">
What is your character name?<BR>
<input type="text" size="20" maxlength="40" name="name">
....
....
....
<textarea rows="5" cols="40" wrap="physical" name="questionten">
</textarea><BR><BR>
<input type="submit" value="Submit Answers">
</form>

I thought I then had to have it go to post.php. If this is all in a php file I am not sure how that would work. Can it post to itself?

etias

12:07 am on Feb 16, 2006 (gmt 0)

10+ Year Member



Oops, forgot something. After an admin goes over the test scores they hit a delete button to delete the .html file. Each person only ever takes the test once. If I had a database then there'd only ever be a name or two in there at a time. Also, I don't see us ever becoming HUGE so we might have one or two people a week take the test so there should be the name.html files in the directory for maybe 10-20 minutes each

jatar_k

1:36 am on Feb 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when the values are posted to 'post.php' you can do whatever you like with them.

write them to file
write them to a database
send them in email

the only restriction is what you can code ;)

>> Also, I don't see us ever becoming HUGE

no one ever does but who knows :)

alright, given the low traffic then maybe using a single file might be better. A single csv file would work.

1. when someone takes the test it just appends a new row of data to the file
2. when an admin wants to view it, a script loops through and grabs the rows from the file, when they are done the row can be deleted

as far as reading from CSV maybe try this thread from our Library [webmasterworld.com]

[webmasterworld.com...]