raif Posted June 11, 2007 Share Posted June 11, 2007 Example URL: http://www.anyolddomainname.com/myfolder/subfolder/folder_name_of_changing_length/one_of_four_names.php Element required : folder_name_of_changing_length Accepting that only the first part of the URL is fixed (http://www.anyolddomainname.com/myfolder/subfolder/), how do I obtain 'folder_name_of_changing_length' ? I'm have a script that I've written that is not especially elegant. Its objective is to generate a menu that includes links that carry variables based on querying a database dependent on the folder name and the file name. I started with this: <? include_once('../includes/db_connect.php'); $url_ext = getenv('SCRIPT_NAME'); $start_string = substr($url_ext, 0, 20); $end_string = strtok(getenv('SCRIPT_NAME'), "/" +1); $end_string = substr($end_string, strrpos($end_string, "/")); $pattern = '/\/myfolder\/subfolder\//'; $replacement = ""; $final_string = preg_replace($pattern, $replacement, $url_ext); $apattern = '/\//'; $areplacement = ""; $afinal_string = preg_replace($apattern, $areplacement, $final_string); //echo $afinal_string; if ($end_string == '/one_of_four_names.php') { $newpattern = '/one_of_four_names.php'; $newreplacement = ""; $folder = preg_replace($newpattern, $newreplacement, $afinal_string); $result = mysql_query(...... I'm using strtok, substr, preg_replace with IF, ELSE IF This works but seems a little clunky!? I looked through the pages here and Googled the subject but can't find anything obvious. I've also read the guidelines about posting these kind of questions and hope that I'm not off the mark with my query. Quote Link to comment https://forums.phpfreaks.com/topic/55067-solved-simple-getenv-and-string-manipulation/ Share on other sites More sharing options...
kathas Posted June 11, 2007 Share Posted June 11, 2007 <?php $url = $_SERVER['REQUEST_URI']; $url_parts = explode('/',$url); $folder = (count($url_parts)>1) ? $url_parts[0] : 'No folder it is just a file from the root dir' ; ?> That should get you the changing length folder... Quote Link to comment https://forums.phpfreaks.com/topic/55067-solved-simple-getenv-and-string-manipulation/#findComment-272214 Share on other sites More sharing options...
raif Posted June 11, 2007 Author Share Posted June 11, 2007 That's great! Thanks kathas! You can see I'm just trying to get a handle on this stuff. I guessed there was probably a simple approach - such is life as a newbie... Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/55067-solved-simple-getenv-and-string-manipulation/#findComment-272217 Share on other sites More sharing options...
raif Posted June 11, 2007 Author Share Posted June 11, 2007 Just to let you know - I've implemented that and it's cut my script down very nicely indeed! Smaller file and quicker processing Cheers Quote Link to comment https://forums.phpfreaks.com/topic/55067-solved-simple-getenv-and-string-manipulation/#findComment-272227 Share on other sites More sharing options...
kathas Posted June 11, 2007 Share Posted June 11, 2007 Thanks... i just read your code and i see you used regular expressions... you should know that they are the slowest string search... they should only be implemented if there is no other way (which is in fact many times)... Regards Kathas Quote Link to comment https://forums.phpfreaks.com/topic/55067-solved-simple-getenv-and-string-manipulation/#findComment-272230 Share on other sites More sharing options...
raif Posted June 11, 2007 Author Share Posted June 11, 2007 Thanks... i just read your code and i see you used regular expressions... you should know that they are the slowest string search... they should only be implemented if there is no other way (which is in fact many times)... Regards Kathas "What can I say" ...lesson learned ...hopefully Quote Link to comment https://forums.phpfreaks.com/topic/55067-solved-simple-getenv-and-string-manipulation/#findComment-272281 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.