Solved

Find pattern in PHP string

Posted on 2013-01-03
6
461 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Session time out quickly. 6 48
Group By Function Required in PDF Output 21 60
Dump data from mysql to xls php 10 51
Testing a list of Emails for validity 4 29
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 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