Solved

Find pattern in PHP string

Posted on 2013-01-03
6
457 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
  • 3
  • 2
6 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

810 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