Jump to content

Recommended Posts

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.

 

 

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.