Jump to content

How to get the last characters of a URL?


redmike

Recommended Posts

Hi guys!

 

I am learning PHP now and I am enjoying it. Im not an I.T. graduate that is why Im having very difficult time to understand  codes.

 

My problem is how to get the last character of a URL that I get using another php code. I can already post the URL on my page but it displays all the URL of the certain page that I get.

 

Example: the URL is "http://mysite.com/page_1/pp1/?lang=zh" I only want to get the "?lang=zh". I am working under 3 languages and I want to get only that last part of the URL for me to continue my work. I dont exactly know what string or filtering I will do to get that part only.

 

Please help me guys. I will appreciate all your comments here.

Link to comment
Share on other sites

What exactly do you need that part for? There may very well be something simpler.

 

Hi sir!

 

Because I have images  (that has english embedded). I also have same images that has chinese/taiwanese characters embeded.  I want to get the last part "?lang=zh" because i have code that if the URL has "lang-zh" it will get the chinese images.

 

heres my sample code that i use.

 

< ?

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
} 

else {  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; }
return $pageURL;
}

$lang= curPageURL();

if ($lang=="http://www.website.com/?lang=zh-hans")	
echo "<a href=\"#\" border=\"0\"><img src=\"http://www.website.com/cn/visa-2.png\" ></a>";

elseif ($lang=="http://www.website.com/?lang=zh-hant")
echo "<a href=\"#\" border=\"0\"><img src=\"http://www.website.com/cnt/visa-3.png\" ></a>";

else 
echo "<a href=\"#\" border=\"0\"><img src=\"http://www.website.com/en/visa.png\" ></a>";
? >

 

This code is okay only under the index.php page. But when im under the inside pages it doesnt work anymore unless I manually add all the needed pages to be translated with images.

 

I have lots of pages to translate. So I think It would be more easier for me if I only get the "?lang=zh-hant" for taiwan images and "?lang=zh-hans" for chinese images.

 

Am I making it clear to you? Actually I dont know how to explain it using programmer's language but Im pretty sure Im explaining it using leighman's.

 

:(

Link to comment
Share on other sites

The most common usage of the query string part of a url is to retrieve the part you want from within the $_GET array. Parsing the string yourself is almost always un-required.

 

I'm quite sure your going about this the wrong way.

Link to comment
Share on other sites

The most common usage of the query string part of a url is to retrieve the part you want from within the $_GET array. Parsing the string yourself is almost always un-required.

 

I'm quite sure your going about this the wrong way.

 

so you mean this is wrong even if it works well on my part? I added this on the above script:

 

 

$lang= curPageURL();

$query = parse_url($lang, PHP_URL_QUERY );

 

if ($query=="lang=zh-hans")

 

 

Link to comment
Share on other sites

The most common usage of the query string part of a url is to retrieve the part you want from within the $_GET array. Parsing the string yourself is almost always un-required.

 

I'm quite sure your going about this the wrong way.

 

so you mean this is wrong even if it works well on my part? I added this on the above script:

 

 

$lang= curPageURL();

$query = parse_url($lang, PHP_URL_QUERY );

 

if ($query=="lang=zh-hans")

 

Your going about it the long way. Whats wrong with....

 

if ($_GET['lang'] == 'zh-hans') {
  // do whatever.
}

 

Of course, you should also check lang is set first....

 

 

if (isset($_GET['lang'] && $_GET['lang'] == 'zh-hans') {
  // do whatever.
}

 

This is why I asked what exactly you where planning on doing with the data in my first reply. There really is no need to parse the url yourself.

Link to comment
Share on other sites

Yes, in this case I can't see that the curPageURL() function is needed at all.  Maybe it's used somewhere else in your script, but here it is useless:

 

if(isset($_GET['lang'])) {
   switch($_GET['lang']) {
      case 'zh-hans':
         echo "<a href=\"#\" border=\"0\"><img src=\"/cn/visa-2.png\" ></a>";
         break;

      case 'zh-hant':
         echo "<a href=\"#\" border=\"0\"><img src=\"/cnt/visa-3.png\" ></a>";
         break;

      default:
         echo "<a href=\"#\" border=\"0\"><img src=\"/en/visa.png\" ></a>";
         break;
   }
}

 

If you do this frequently, then you'll probably have better luck by doing this check at the top of the page, maybe in a header file and then assigning the language to a session var to be used later.

Link to comment
Share on other sites

The most common usage of the query string part of a url is to retrieve the part you want from within the $_GET array. Parsing the string yourself is almost always un-required.

 

I'm quite sure your going about this the wrong way.

 

so you mean this is wrong even if it works well on my part? I added this on the above script:

 

 

$lang= curPageURL();

$query = parse_url($lang, PHP_URL_QUERY );

 

if ($query=="lang=zh-hans")

 

Your going about it the long way. Whats wrong with....

 

if ($_GET['lang'] == 'zh-hans') {
  // do whatever.
}

 

Of course, you should also check lang is set first....

 

 

if (isset($_GET['lang'] && $_GET['lang'] == 'zh-hans') {
  // do whatever.
}

 

This is why I asked what exactly you where planning on doing with the data in my first reply. There really is no need to parse the url yourself.

 

Your missing the closing parethisis for the isset on your code.

Link to comment
Share on other sites

Yes, in this case I can't see that the curPageURL() function is needed at all.  Maybe it's used somewhere else in your script, but here it is useless:

 

if(isset($_GET['lang'])) {
   switch($_GET['lang']) {
      case 'zh-hans':
         echo "<a href=\"#\" border=\"0\"><img src=\"/cn/visa-2.png\" ></a>";
         break;

      case 'zh-hant':
         echo "<a href=\"#\" border=\"0\"><img src=\"/cnt/visa-3.png\" ></a>";
         break;

      default:
         echo "<a href=\"#\" border=\"0\"><img src=\"/en/visa.png\" ></a>";
         break;
   }
}

 

If you do this frequently, then you'll probably have better luck by doing this check at the top of the page, maybe in a header file and then assigning the language to a session var to be used later.

 

I tried this one and yes its working only in "zh-hant" and zh-hans". But the default displays nothing. That is why I think parsing the url works well. 

 

I dont know how to make the default works. Is it maybe because Im using includes instead of echo to display everything?

 

 

I understand yes, curPageURL() function doesn't do anything here. I didnt use it anywhere too. But I find more useful that the "_GET".

 

if(isset($_GET['lang'])) {
   switch($_GET['lang']) {
      case 'zh-hans':
         	include (TEMPLATEPATH . '/branches-cn.php');  //branches pages contains the translated images
         break;

      case 'zh-hant':
        	include (TEMPLATEPATH . '/branches-tcn.php'); 
         break;

      default:
         include (TEMPLATEPATH . '/branches.php'); //This is my english page 
         break;
   }
}

 

Is there something missing here? Why is the english images not displayed?

Link to comment
Share on other sites

try

if(isset($_GET['lang'])) {
   switch($_GET['lang']) {
      case 'zh-hans':
         	include (TEMPLATEPATH . '/branches-cn.php');  //branches pages contains the translated images
         break;

      case 'zh-hant':
        	include (TEMPLATEPATH . '/branches-tcn.php'); 
         break;

      default:
         include (TEMPLATEPATH . '/branches.php'); //This is my english page 
         break;
   }
} else  include (TEMPLATEPATH . '/branches.php');

Link to comment
Share on other sites

try

if(isset($_GET['lang'])) {
   switch($_GET['lang']) {
      case 'zh-hans':
         	include (TEMPLATEPATH . '/branches-cn.php');  //branches pages contains the translated images
         break;

      case 'zh-hant':
        	include (TEMPLATEPATH . '/branches-tcn.php'); 
         break;

      default:
         include (TEMPLATEPATH . '/branches.php'); //This is my english page 
         break;
   }
} else  include (TEMPLATEPATH . '/branches.php');

 

Great! this works!

Link to comment
Share on other sites

try

if(isset($_GET['lang'])) {
   switch($_GET['lang']) {
      case 'zh-hans':
         	include (TEMPLATEPATH . '/branches-cn.php');  //branches pages contains the translated images
         break;

      case 'zh-hant':
        	include (TEMPLATEPATH . '/branches-tcn.php'); 
         break;

      default:
         include (TEMPLATEPATH . '/branches.php'); //This is my english page 
         break;
   }
} else  include (TEMPLATEPATH . '/branches.php');

 

Good catch.

Link to comment
Share on other sites

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.