redmike Posted August 14, 2010 Share Posted August 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/ Share on other sites More sharing options...
trq Posted August 14, 2010 Share Posted August 14, 2010 What exactly do you need that part for? There may very well be something simpler. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099130 Share on other sites More sharing options...
stuartbates Posted August 14, 2010 Share Posted August 14, 2010 Use the parse url function: http://uk3.php.net/manual/en/function.parse-url.php Would look like this: // To print echo parse_url($url, PHP_URL_QUERY ); // To assign $query = parse_url($url, PHP_URL_QUERY ); Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099131 Share on other sites More sharing options...
AbraCadaver Posted August 14, 2010 Share Posted August 14, 2010 echo $_GET['lang']; Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099213 Share on other sites More sharing options...
redmike Posted August 16, 2010 Author Share Posted August 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099753 Share on other sites More sharing options...
redmike Posted August 16, 2010 Author Share Posted August 16, 2010 Use the parse url function: http://uk3.php.net/manual/en/function.parse-url.php Would look like this: // To print echo parse_url($url, PHP_URL_QUERY ); // To assign $query = parse_url($url, PHP_URL_QUERY ); Wow this answers my problem! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099756 Share on other sites More sharing options...
redmike Posted August 16, 2010 Author Share Posted August 16, 2010 echo $_GET['lang']; thanks! this also works! Im so happy! Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099757 Share on other sites More sharing options...
redmike Posted August 16, 2010 Author Share Posted August 16, 2010 I still dont know to match the URL and characters that I get after parsing. I still get the english images only... Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099759 Share on other sites More sharing options...
redmike Posted August 16, 2010 Author Share Posted August 16, 2010 Oh i see! its working now. Sorry I messed up. I use this to assign. : // To assign $query = parse_url($url, PHP_URL_QUERY ); thanks stuart! Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099764 Share on other sites More sharing options...
trq Posted August 16, 2010 Share Posted August 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099769 Share on other sites More sharing options...
redmike Posted August 16, 2010 Author Share Posted August 16, 2010 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") Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099770 Share on other sites More sharing options...
trq Posted August 16, 2010 Share Posted August 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099776 Share on other sites More sharing options...
AbraCadaver Posted August 16, 2010 Share Posted August 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099851 Share on other sites More sharing options...
newbtophp Posted August 16, 2010 Share Posted August 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1099864 Share on other sites More sharing options...
redmike Posted August 17, 2010 Author Share Posted August 17, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1100119 Share on other sites More sharing options...
sasa Posted August 17, 2010 Share Posted August 17, 2010 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'); Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1100173 Share on other sites More sharing options...
redmike Posted August 17, 2010 Author Share Posted August 17, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1100200 Share on other sites More sharing options...
AbraCadaver Posted August 17, 2010 Share Posted August 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210697-how-to-get-the-last-characters-of-a-url/#findComment-1100304 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.