Link to home
Start Free TrialLog in
Avatar of SSupreme
SSupremeFlag for Belarus

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/PHP/newQuestionWizard.jsp

I need to only bold name;

experts-exchange.com/Web_Development/Web_Languages-Standards/PH/newQuestionWizard.jsp

I don't need names of second (etc) directory.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

It looks like you can use $_SERVER["SCRIPT_NAME"] to get it.  Demo code below.
<?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 = $_SERVER["SCRIPT_NAME"];
echo $nname."<br>";

$pieces = explode("/", $nname);
foreach($pieces as $vstring) {
	if(strlen($vstring) == 2) echo $vstring;
	}
?>

</body>
</html>

Open in new window

Avatar of SSupreme

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>

Open in new window

ok,
experts-exchange.com/Web_Development/Web_Languages-Standards/PH/newQuestionWizard.jsp

I don't need names of second (etc) directory.
If I would have mysite.com/WD/ph/th/lm.html
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>

Open in new window


<?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>

Open in new window

I need only WD from mysite.com/WD/ph/th/lm.html
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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
<?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);

Open in new window

"I need only WD from mysite.com/WD/ph/th/lm.html"

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.
DaveBaldwin, I know don't worry, I wanted to say this:
experts-exchange.com/Web_Development/Web_Languages-Standards/PH/newQuestionWizard.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_NAME"],0,strrpos($_SERVER["SCRIPT_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
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';
}

Open in new window


I wonder which one is faster, if I would use something like this: substr($_SERVER["SCRIPT_NAME"],0,strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
Regex 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/maintainability and performance.
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_Development/Web_Languages-Standards/PH/newQuestionWizard.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.
Genius indeed.
I think I understand now.