SSupreme
asked on
URL directory echo2
I need to extract name of first directory from URL if its length =2, else NULL for example:
experts-exchange.com/WD/Web_Languages-Standards/P HP/newQues tionWizard .jsp
I need to only bold name;
experts-exchange.com/Web_D evelopment /Web_Langu ages-Stand ards/PH/newQuestionWizard.jsp
I don't need names of second (etc) directory.
experts-exchange.com/WD/Web_Languages-Standards/P
I need to only bold name;
experts-exchange.com/Web_D
I don't need names of second (etc) directory.
ASKER
No, I cannot. Assume it is a string, how can I get value from it?
Basically the same way.
<?php
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP Get Name</title>
</head>
<body>
<?php
$nname = "experts-exchange.com/WD/Web_Languages-Standards/PHP/newQuestionWizard.jsp";
echo $nname."<br>";
$pieces = explode("/", $nname);
foreach($pieces as $vstring) {
if(strlen($vstring) == 2) echo $vstring;
}
?>
</body>
</html>
ASKER
ok,
l
I guess it will return: WDphth
and more if first directory is not = length 2, it should be NULL
experts-exchange.com/Web_DIf I would have mysite.com/WD/ph/th/lm.htmevelopment /Web_Langu ages-Stand ards/PH/ne wQuestionW izard.jsp
I don't need names of second (etc) directory.
I guess it will return: WDphth
and more if first directory is not = length 2, it should be NULL
Then just assign it to a variable and 'break' out of the 'foreach loop'.
<?php
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP Get Name</title>
</head>
<body>
<?php
$nname = "experts-exchange.com/WD/Web_Languages-Standards/PHP/newQuestionWizard.jsp";
echo $nname."<br>";
$pieces = explode("/", $nname);
foreach($pieces as $vstring) {
if(strlen($vstring) == 2) {
$result = $vstring;
break;
}
}
echo $result;
?>
</body>
</html>
<?php
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP Get Name</title>
</head>
<body>
<?php
$nname = "experts-exchange.com/WD/Web_Languages-Standards/PHP/newQuestionWizard.jsp";
echo $nname."<br>";
$result = NULL; // or ""
$pieces = explode("/", $nname);
foreach($pieces as $vstring) {
if(strlen($vstring) == 2) {
$result = $vstring;
break;
}
}
echo $result;
?>
</body>
</html>
ASKER
I need only WD from mysite.com/WD/ph/th/lm.htm l
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@SSupreme: I believe you are making this harder than it needs to be.
If you are using sub-directories of the web root to establish different language web sites, the design pattern would be like this:
http://www.domain.com/XX/path/to/script.php
In this design pattern you would not need to but you would need XX since XX would be the ISO-639-1 language code.
http://www.loc.gov/standards/iso639-2/php/code_list.php
So looking for a two-character string is not really what you would need. A more refined search would be to look for the first two characters of REQUEST_URI, after the leftmost DIRECTORY SEPARATOR (if present) has been removed. You might use substr() or you might use something like this code snippet. I think this is less brittle than depending on the exact values in REQUEST_URI -- these may differ from one system to another.
See http://www.laprbass.com/en/internal/RAY_temp_ssupreme.php?foo=bar for an example. I will leave that online for a while so you can see what it does.
HTH, ~Ray
If you are using sub-directories of the web root to establish different language web sites, the design pattern would be like this:
http://www.domain.com/XX/path/to/script.php
In this design pattern you would not need to but you would need XX since XX would be the ISO-639-1 language code.
http://www.loc.gov/standards/iso639-2/php/code_list.php
So looking for a two-character string is not really what you would need. A more refined search would be to look for the first two characters of REQUEST_URI, after the leftmost DIRECTORY SEPARATOR (if present) has been removed. You might use substr() or you might use something like this code snippet. I think this is less brittle than depending on the exact values in REQUEST_URI -- these may differ from one system to another.
See http://www.laprbass.com/en/internal/RAY_temp_ssupreme.php?foo=bar for an example. I will leave that online for a while so you can see what it does.
HTH, ~Ray
<?php // laprbass.com/en/internal/RAY_temp_ssupreme.php
error_reporting(E_ALL);
$lang_code = current(explode(DIRECTORY_SEPARATOR, ltrim($_SERVER["REQUEST_URI"], DIRECTORY_SEPARATOR)));
// SHOW THE LANGUAGE CODE
var_dump($lang_code);
"I need only WD from mysite.com/WD/ph/th/lm.htm l"
That last code I posted does that... after it displays the initial string so you can see what you're working with. All you have to do is kill the 'echo' and $result has what you asked for.
That last code I posted does that... after it displays the initial string so you can see what you're working with. All you have to do is kill the 'echo' and $result has what you asked for.
ASKER
DaveBaldwin, I know don't worry, I wanted to say this:
I think your last solution would output: PH, but it should! output: null. Does your solution meat this requirement?
kaufmed, your solution is twice slimmer than solution of DaveBaldwin and does what it should.
I wonder which one is faster, if I would use something like this: substr($_SERVER["SCRIPT_NA ME"],0,str rpos($_SER VER["SCRIP T_NAME"]," /")+1);
Ray_Paseur, I'd like mysite start from mysite.com/index.php, but I guess your code will output: index.php, instead of null and if null then default, so I think it will be hard as well. You remember it should go there $lang = 'en'; //Default Language
experts-exchange.com/Web_Development /Web_Langu ages-Stand ards/PH/ne wQuestionW izard.jsp
I don't need names of second (etc) directory.
I think your last solution would output: PH, but it should! output: null. Does your solution meat this requirement?
kaufmed, your solution is twice slimmer than solution of DaveBaldwin and does what it should.
I wonder which one is faster, if I would use something like this: substr($_SERVER["SCRIPT_NA
Ray_Paseur, I'd like mysite start from mysite.com/index.php, but I guess your code will output: index.php, instead of null and if null then default, so I think it will be hard as well. You remember it should go there $lang = 'en'; //Default Language
No, because your original statement said to return two-letter names. It said nothing about a list of two-letter names.
this shud work ...
$url = "experts-exchange.com/Web_Development/Web_Languages-Standards/PH/newQuestionWizard.jsp"
$arr = explode("/",$url);
array_pop($arr);
$dir = array_pop($arr);
if(strlen($dir) == 2){
echo $dir;
}else{
echo 'null';
}
I wonder which one is faster, if I would use something like this: substr($_SERVER["SCRIPT_NARegex always incurs a bit of overhead. It is often difficult to gauge performance between a regex and non-regex algorithm. I can only suggest trying each on your system to see which performs better. Writing code is often a delicate balance between readability/maintainabilitME"],0,str rpos($_SER VER["SCRIP T_NAME"]," /")+1);
ASKER
DaveBaldwin,
basically original statements are:
extract name of first directory from URL if its length =2, else NULL;
I don't need names of second (etc) directory.
your first solution does this:
extract name of any directories with length =2;
your second solution does this:
extract name of first directory with length =2;
But this example qualify:
experts-exchange.com/Web_D evelopment /Web_Langu ages-Stand ards/PH/ne wQuestionW izard.jsp
I don't need names of second (etc) directory.
that I need first directory not first with length =2.
It is hard to explain what I need exactly, I did my best and I am sorry if you didn't understand the question.
basically original statements are:
extract name of first directory from URL if its length =2, else NULL;
I don't need names of second (etc) directory.
your first solution does this:
extract name of any directories with length =2;
your second solution does this:
extract name of first directory with length =2;
But this example qualify:
experts-exchange.com/Web_D
I don't need names of second (etc) directory.
that I need first directory not first with length =2.
It is hard to explain what I need exactly, I did my best and I am sorry if you didn't understand the question.
ASKER
Genius indeed.
I think I understand now.
Open in new window