monkeytooth Posted May 14, 2009 Share Posted May 14, 2009 I am working on a function for a site that I am working. The function itself is working fine minus one element. What the concept is, is a user clicks on a link to allow this function to go to its corresponding loading page, runs it script, and then sends the user back to where they were since the ability to use the function is spread out through out the site there is no static means per say of doing it. I have been attempting to use $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']; To catch the last URL prior to running the function, but I keep getting stuck in an endless loop cause the function looks for a specfic var in the URL to activate and run. So using $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']; catches that particular var as well so I need a way to trim it out if its in the url but only after the function runs. the var is "theme=xxxxxxx" xxxxxx being anything from 3-16 char long. I have made it so that it appends to the end of the URI everytime however. So anyone any suggestions or ideas as to how I can do this? Quote Link to comment https://forums.phpfreaks.com/topic/158121-solved-trim-a-url-if/ Share on other sites More sharing options...
Adam Posted May 14, 2009 Share Posted May 14, 2009 Could use a regular expression? Something like... $url = $_SERVER['PHP_SELF'] . "?" . preg_replace('/theme=[^&]+/', '', $_SERVER['QUERY_STRING']); Quote Link to comment https://forums.phpfreaks.com/topic/158121-solved-trim-a-url-if/#findComment-834051 Share on other sites More sharing options...
monkeytooth Posted May 14, 2009 Author Share Posted May 14, 2009 Thank you.. I ended up playing a bit and with a little searching combined with the playing I came to this.. function remove_querystring_var($url, $key) { $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); $url = substr($url, 0, -1); return ($url); } $backlink2use = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']; $backlink2use = remove_querystring_var($backlink2use, "theme"); Quote Link to comment https://forums.phpfreaks.com/topic/158121-solved-trim-a-url-if/#findComment-834057 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.