• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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.
0
SSupreme
Asked:
SSupreme
  • 7
  • 6
  • 3
  • +2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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

0
 
SSupremeAuthor Commented:
No, I cannot. Assume it is a string, how can I get value from it?
0
 
Dave BaldwinFixer of ProblemsCommented:
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SSupremeAuthor Commented:
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
0
 
Dave BaldwinFixer of ProblemsCommented:
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

0
 
Dave BaldwinFixer of ProblemsCommented:

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

0
 
SSupremeAuthor Commented:
I need only WD from mysite.com/WD/ph/th/lm.html
0
 
käµfm³d 👽Commented:
Actually, I reread you OP here, and I would need to modify my previous suggestion.

preg_match('#(?<=/)[^/]{2}(?=/)#', $input, $result);

var_dump($result);

Open in new window

0
 
Ray PaseurCommented:
@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

0
 
Dave BaldwinFixer of ProblemsCommented:
"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.
0
 
SSupremeAuthor Commented:
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
0
 
Dave BaldwinFixer of ProblemsCommented:
No, because your original statement said to return two-letter names.  It said nothing about a list of two-letter names.
0
 
Sandeep KothariProject LeadCommented:
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


0
 
käµfm³d 👽Commented:
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.
0
 
SSupremeAuthor Commented:
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.
0
 
SSupremeAuthor Commented:
Genius indeed.
0
 
Dave BaldwinFixer of ProblemsCommented:
I think I understand now.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now