?
Solved

Find pattern in PHP string

Posted on 2013-01-03
6
Medium Priority
?
466 Views
Last Modified: 2013-01-03
Hi,

In order to facilitate multiple countries, each country will have a website URL as below:

http://www.somewhere.com/ie/index.html       (for Ireland)
http://www.somewhere.com/fr/index.html       (for France)

I'm trying to extract the 2 character country code between the forwardslashes (it will always be 2 characters long). I'm guessing I need a Regexp but I have no idea how to use them.

In English, the pattern would be /XX/ where XX is what needs to be stored in a PHP variable. The position of the string /XX/ would not be fixed as I need this working using http://localhost/folder as well as http://server/folder and http://www........

Can somebody please provide some direction on how I can get started?

Thanks in advance
0
Comment
Question by:kbit
[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
  • 3
  • 2
6 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 38740284
Assuming you have in your urls only and only one group of two letters you can use this:

preg_match('%(?<=/)\w\w(?=/)%sim', $url, $matches);
$country = $matches[0];
echo $country;

Cheers
0
 
LVL 12

Expert Comment

by:Chris
ID: 38740325
Here is another method (which I find easier to understand as regexps frankly hurt my head). This will work provided the country code always comes immediately after the DNS name.

<?php

$string = "http://www.somewhere.com/ie/index.html";

$resultArray = explode("/",$string,5);
$countryCode = $resultArray[3];
echo "Country Code is " . $countryCode;

?>
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38740413
@Demented_Goose: with no offense, but your code will work fine only if you know exactly where the searched token is within the string, but if that place can change like:

http://localhost/folder/fr/index.html
http://server/folder/fr/index.html

In these cases, your code returns 'folder' as Country Code. In addition, limiting to 5 the exploding process make the code fail with long url such as:

http://localhost/folder/subfolder/subsubfolder/fr/anothersubfolder/index.html

Cheers
0
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

Author Closing Comment

by:kbit
ID: 38740613
Many thanks for such a quick and thorough reply, this worked perfectly
0
 
LVL 12

Expert Comment

by:Chris
ID: 38740957
@marqusG. This is correct and I did actually explain this in my original post - "This will work provided the country code always comes immediately after the DNS name.".

At the end of the day, if it is known that the country code will always be after the hostname then the explode method will work every time.

I could take your example and say "What if there's a URL of http://host/12/uk/somethingelse.htm ?"

The country code that should be extracted is "uk" but your regexp will return "12" - not exactly ideal. At the end of the day both methods work as long as you can be sure of the format of the URL.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38741686
@Demented_Goose: you're right, I didn't consider that case and it made me think about the best way to manage all possible situations. Maybe a good way is the following:

$url = "http://host/12/uk/somethingelse.htm ";
preg_match('%(?<=/)[A-Za-b]{2}(?=/)%sim', $url, $matches);
$country = $matches[0];
echo $country;

Open in new window


This way, only letters will be found.

Cheers
Marco
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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
This article discusses how to create an extensible mechanism for linked drop downs.
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

762 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