Solved

Get country acronym from URL (php)

Posted on 2010-09-14
7
482 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 74

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 74

Expert Comment

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

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
Comment Utility

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

Open in new window

0
 
LVL 3

Author Closing Comment

by:Pedro Chagas
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now