[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Find pattern in PHP string

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
kbit
Asked:
kbit
  • 3
  • 2
1 Solution
 
Marco GasiFreelancerCommented:
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
 
ChrisCommented:
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
 
Marco GasiFreelancerCommented:
@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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kbitAuthor Commented:
Many thanks for such a quick and thorough reply, this worked perfectly
0
 
ChrisCommented:
@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
 
Marco GasiFreelancerCommented:
@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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now