Jump to content

[ HELP! ] exclude file endings and file start!?!


ztealmax

Recommended Posts

Hello again forum, i have a question that i hope you think is simple:

 

Have this code that excludes file endings that ends with .news

 

Is it possible to get it to exclude anything after first filename

for example if the filename is:

dragons.about.news

and when i use the script i want it only to show

dragons (dragons could be anyname i was hoping i could use like * or something

however if i modify this script further i would like to get it to show the following

about and this could be anything also so i would like it to show whatever is written between

first text and last text

 

here is my small code

<?php
  
  $dir = "news/";
  $dh  = opendir($dir);
  while (false !== ($filename = readdir($dh))) 
{
   $excludenews = '/\.news$/';

   if(preg_match($excludenews,$filename))
   {
      /*    You can proceed because the file has the extension .zip       */
   $filename = ereg_replace($dir, "", "$filename");
  $filename = ereg_replace(".news", "", "$filename");
echo "<a href='?content=read&contents=".$dir."Catogory.".$filename.".desc'>" . $filename . "</a><br>";
   }
}

?>

Does this make any sense? Ask me if you dont understand what im asking or

if you want to know more about how im thinking

 

Cheers

Martin

Link to comment
Share on other sites

There are several ways to accomplish what you are looking for, so I'm not sure what will be best for you. My first impulse is to just use the split method.

$filename = "dragon.about.news";

$namearray = split(".", $filename);

echo $namearray[0]."<br />";
echo $namearray[1];

 

output would be:

 

dragon

about

 

Link to comment
Share on other sites

come on now, I'm not gonna make any bad comments here but your are trying to be at least a hobby programmer right?

Both solutions above will work with any input string they simple break the string into parts via the split or the explode function. Though I have to admit the documentation does say that explode is a better choice than split if you do not need to use regular expressions.

Link to comment
Share on other sites

come on now, I'm not gonna make any bad comments here but your are trying to be at least a hobby programmer right?

Both solutions above will work with any input string they simple break the string into parts via the split or the explode function. Though I have to admit the documentation does say that explode is a better choice than split if you do not need to use regular expressions.

 

well as i see the code you have to know what filename it is before, and as i dont know that until its done

$string = "dragons.about.news";

 

states its a file name that exists right?

but ofc i could get it to get names from filelisting

 

and yes im new to this thats why im trying to get hints and tips how to do things.

 

 

Link to comment
Share on other sites

I simply used a bit of code to demonstrate the use of the split function. I just assigned the variable to a static string value, you of course would have to derive the value as needed for your specific application.

 

As you move forward in the learning process it will be rare to get someone to show you exactly the answer to your specific problem. More often than not they will point you to or give you the tools/info neccessary to accomplish the task.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.