Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get country acronym from URL (php)

Posted on 2010-09-14
7
Medium Priority
?
510 Views
Last Modified: 2012-05-10
Hi E's, I need to get to inside a variable the country acronym inside URL.
The URL was dynamic, and in all cases the acronym appear in URL.
The URL can have this address:
www.example.com/en/index.php
www.example.com/en/
www.example.com/en/index.php?getvariables=any_values
www.example.com

What I want get is 'en', and 'en' can be any country acronym (like pt, es, it, no). If the client just type www.example.com or www.example.com/index.php (without acronym) the script can give me variable with value 0.

How I do this?

Regards, JC
0
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1600 total points
ID: 33674345
How about this pattern:

    (?<=\/)[a-z]{2}(?=\/)

Example:
<?php
	$sites = array("www.example.com/en/index.php",
					"www.example.com/en/",
					"www.example.com/en/index.php?getvariables=any_values",
					"www.example.com");

	foreach ($sites as $s)
	{
		print $s;
		preg_match("/(?<=\/)[a-z]{2}(?=\/)/", $s, $matches);
		var_dump($matches);
		print "<br/>";
	}
?>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33674365
P.S.

The backslashes in "\/" are not part of the pattern, they are escapes to the forward slashes. If you change your pattern delimiter, you would not need the backslashes.
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33678120
Hi @kaufmed, your code work well.
I don't tell when I open the question, but have another possible address, it is 'www.example.com/pt', without '/' in the end.
Can you please change the RE in your code?

P.s My apologizes for don't tell that in the first post.

Regards, JC
0
Independent Software Vendors: 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!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33678384
Try this:
(?<=www\.example\.com\/)[a-z]{2}(?=\/|$)

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33680230
Hi, Like I tell you your first example work well, just don't work with 'www.example.com/pt', without '/' in the end. Your first example work with http://www.exam....... and just with http://examp...... (without www).
Your new example work with 'www.example.com/pt' (http://www.examp......), but not work without www, so if the address was http://example.com/en/index.php don't work, just work if was http://www.example.com/en/index.php (with www).
Sometime customers or the system assume the address without 'www', and the system have to be prepare for that fact.
Please view the RE for URL with www and without www.

Regards, JC
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1600 total points
ID: 33683264

(?<=\/)[a-z]{2}(?=(?:\/|$))

Open in new window

0
 
LVL 3

Author Closing Comment

by:Pedro Chagas
ID: 33683577
Join the two posts:
<?php
      $sites = array("www.example.com/en/index.php",
                              "www.example.com/en/",
                              "www.example.com/en/index.php?getvariables=any_values",
                              "www.example.com");

      foreach ($sites as $s)
      {
            print $s;
            preg_match("/(?<=\/)[a-z]{2}(?=(?:\/|$))
/", $s, $matches);
            var_dump($matches);
            print "";
      }
?>
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question