Solved

Get country acronym from URL (php)

Posted on 2010-09-14
7
485 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
  • 4
  • 3
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 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