ztealmax Posted March 10, 2009 Share Posted March 10, 2009 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 Quote Link to comment Share on other sites More sharing options...
DocSeuss Posted March 10, 2009 Share Posted March 10, 2009 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 Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 10, 2009 Share Posted March 10, 2009 Quick and dirty: $string = "dragons.about.news"; $words = explode(".",$string); array_pop($words); array_shift($words); $string1 = implode(".",$words); echo $string1; Quote Link to comment Share on other sites More sharing options...
ztealmax Posted March 11, 2009 Author Share Posted March 11, 2009 problem is that the filename isnt allways "dragons.about.news" the name is diferent since its created when a new news post is created, any other way to acomplis this? //Cheers Martin Quote Link to comment Share on other sites More sharing options...
DocSeuss Posted March 11, 2009 Share Posted March 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
ztealmax Posted March 11, 2009 Author Share Posted March 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
DocSeuss Posted March 11, 2009 Share Posted March 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
ztealmax Posted March 11, 2009 Author Share Posted March 11, 2009 cheers m8 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.