Solved

Get country acronym from URL (php)

Posted on 2010-09-14
7
484 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to count in a table in php 22 35
Blocking submission of a web-based form 6 20
regex code: words must be 6 to 8 characters long ? 2 14
parse a file and get data out 11 44
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 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