DamienRoche Posted September 20, 2008 Share Posted September 20, 2008 Here's what I want to do: open a file in a directory which has a certain pattern in it's name..then, after opening the file, put the file name into a variable. /// FILE ON SERVER = (1)damien.txt $ptn = "(1)" $lines = file("$ptn___I AM STUCK HERE___.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); /// output is sorted. So, I need to open any file (there will only be one) with that pattern in, maybe by using somekind of wild card..but after opening the file, I need to know what the rest of the name was. I know, I said there would only be one...but that's what I need. Any one got any ideas on how I can tackle this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/ Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 perhaps glob might be of some use? Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/#findComment-646410 Share on other sites More sharing options...
DamienRoche Posted September 20, 2008 Author Share Posted September 20, 2008 Just checked that out..it is of some use and now I am one step closer. so now I have used glob and it has found the name. Let's say the name is (1)damien_full.txt How do I put that file name into a string and then pull out everything between (1) and _full and put it in a variable. So, in essence, how do I get 'damien' from that file name into a variable? Any ideas? Thanks as well! that glob function is incredible! Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/#findComment-646415 Share on other sites More sharing options...
JasonLewis Posted September 20, 2008 Share Posted September 20, 2008 Assuming the number will change, and the text after the _ $file = "(1)damien_full.txt"; preg_match("#\(\d\)(.*?)_\w#i", $file, $matches); echo $matches[1]; Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/#findComment-646426 Share on other sites More sharing options...
DamienRoche Posted September 20, 2008 Author Share Posted September 20, 2008 That last one hasn't worked. I need something relative not absolute. That seems to only work on the exact string (1)damien_full.txt That regexp is seriously above my head. How can I alter it to make this work. If I can use a substr(); it would be great but that seems to only work with numbering the pos in a string. Can I not number the pos based on special characters? namely, the ) and _ in the given string. Any one else got any other ideas? Thanks any way! Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/#findComment-646433 Share on other sites More sharing options...
JasonLewis Posted September 20, 2008 Share Posted September 20, 2008 You can use strpos() to find the location of those. $file = "(1)damien_full.txt"; echo substr($file, strpos($file, ")")+1, strpos($file, "_") - (strpos($file, ")")+1)); Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/#findComment-646439 Share on other sites More sharing options...
DamienRoche Posted September 20, 2008 Author Share Posted September 20, 2008 Genius, my friend! Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/#findComment-646443 Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 hmm.... It seems to me what you are saying is that you have a list of files and you want to search for a file that contains for instance "dam" and that there's only going to be one file with that in it, and it's going to be for example (1)damien_full.txt so you want to be able to extract "damien" from that, but the catch is that file won't necessarily look like that, it could look like (1)damien_full.txt damien_full.txt (1)damienfull.txt damien.txt etc... Only way regex really works is if there are, well, regular expressions to work with. If you are telling us that beyond using glob, virtually everything else in that filename could be variable, then there's really no way to extract your info from it. edit: Okay well apparently that's not exactly what you're saying, as something seemed to work out for you... Quote Link to comment https://forums.phpfreaks.com/topic/125076-solved-open-file-based-on-wild-card-put-file-name-into-string/#findComment-646448 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.