Solved

Get country acronym from URL (php)

Posted on 2010-09-14
7
483 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 count occurrences of each item in an array.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now