nikefido Posted October 30, 2008 Share Posted October 30, 2008 Hi - I need help matching a php file with a query string. The queries will always be: /somefile.php?submit=dj Where "somefile" will be the only changing item. What regex do I need for this? (This seems simple now that I've written it out lol) Quote Link to comment https://forums.phpfreaks.com/topic/130758-need-regex-match-for-filephpquerystring/ Share on other sites More sharing options...
The Little Guy Posted October 30, 2008 Share Posted October 30, 2008 This here? $file = '/somefile.php?submit=dj' if(preg_match("~/(.+?).php\?submit=dj~",$file,$matches)){ echo '<pre>'; print_r($matches); echo '</pre>'; } Quote Link to comment https://forums.phpfreaks.com/topic/130758-need-regex-match-for-filephpquerystring/#findComment-678830 Share on other sites More sharing options...
nrg_alpha Posted October 30, 2008 Share Posted October 30, 2008 This can find the file (doesn't even have to be a php file) in a string, even if that file is contained within subfolders. EDIT - This pattern assumes that the file name can contain any word characters (a-zA-Z0-9_), a space and a dash.. if spaces nor dashes will be in files, you can simply remove those from the pattern. $file = '/someFolder/someOtherFolder/somefile.php?submit=dj'; $replace = 'someOtherFile'; $newFile = preg_replace('#[\w -]+(\.[^?]+)#', "$replace$1", $file); echo $newFile; Ouput: /someFolder/someOtherFolder/someOtherFile.php?submit=dj Quote Link to comment https://forums.phpfreaks.com/topic/130758-need-regex-match-for-filephpquerystring/#findComment-678849 Share on other sites More sharing options...
DarkWater Posted October 31, 2008 Share Posted October 31, 2008 Since he has such a specific string, it would probably be better to use something like: <?php $string = 'somefile.php?submit=dj'; $file = array_shift(explode('?', $string)); echo $file; ?> Quote Link to comment https://forums.phpfreaks.com/topic/130758-need-regex-match-for-filephpquerystring/#findComment-678996 Share on other sites More sharing options...
nrg_alpha Posted October 31, 2008 Share Posted October 31, 2008 Since he has such a specific string, it would probably be better to use something like: <?php $string = 'somefile.php?submit=dj'; $file = array_shift(explode('?', $string)); echo $file; ?> a) you only isolate the file... I *think* the OP wants to change the filename to something else (yet retain the query part?) Maybe I misunderstood what the OP seeks. b) I have made the solution more 'adaptable', (just in case the OP tries to apply it to a string with subfolders). How many times have you provided a solution, only to have the OP come back and throw in a new situation and thus your solution no longer works (this is not your fault.. but rather the OP in question for not explaining ALL situations needed)? And this leads to point c... c) Your solution would not work if there were 'all of a sudden' subfolders... I am seeing so many poorly constructed posed problems by OPs these days, I am trying to think of solutions that can solve some more 'unknowns' thrown into the mix.. may not be perfect.. but might solve a round or two of 'I tried your code against THIS, and it didn't work' kind of thing. If I have misunderstood things, then my apologies. I gathered the filename is to be altered to something else, yet everything else in the string is retained. Quote Link to comment https://forums.phpfreaks.com/topic/130758-need-regex-match-for-filephpquerystring/#findComment-679004 Share on other sites More sharing options...
DarkWater Posted October 31, 2008 Share Posted October 31, 2008 Okay, subfolders: <?php $string = '/some/sub/dirs/somefile.php?submit=dj'; $file = array_shift(explode('?', basename($string))); echo $file; ?> Output: somefile.php But don't get me wrong, I totally see your point. Sometimes things are handled better without regex, like in this case. Quote Link to comment https://forums.phpfreaks.com/topic/130758-need-regex-match-for-filephpquerystring/#findComment-679007 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.