Solved

Find pattern in PHP string

Posted on 2013-01-03
6
451 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 30

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 30

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 30

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now