Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8703219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:49:44+00:00 2026-06-13T02:49:44+00:00

I have the following possible strings: NL DE NL,DE nl DE nl de NL/DE

  • 0

I have the following possible strings:

NL DE 
NL,DE
nl DE
nl de
NL/DE
NL,mismatch,DE

I’m looking for the preg_match that produces the following output given the inputs above.

array(
  [0]=>"NL",
  [1]=>"DE"
);

I’ve tried the following code:

preg_match_all('/(\w{2,2})/ui', $info["country"], $m);

but that seems to also cut up the word mismatch, which is undesired.

The regex should only match two letter country codes, everything else should be ignored.

How can I do this using preg_match in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:49:45+00:00Added an answer on June 13, 2026 at 2:49 am
    // @claudrian Variant
    function SplitCountries($string){
        // Sanity check
        if(!is_string($string)) return false;
        // Split string by non-letters (case insensitive)
        $slices = preg_split('~[^a-z]+~i', trim($string));
        // Keep only 2-letter words
        $slices = preg_grep('~^[a-z]{2}$~i', $slices);
        // Keep uniques
        $slices = array_unique($slices);
        // Done
        return $slices;
    }
    
    // @Wiseguy Variant
    function SplitCountries($string){
        // Sanity check
        if(!is_string($string)) return false;
        // Capture only two letter packs
        if(!preg_match_all('~\\b[a-z]{2}\\b~i', trim($string), $slices)){
            return false;
        }
        // Keep uniques
        $slices = array_unique($slices[0]);
        // Done
        return $slices;
    }
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following possible strings that I need to turn into arrays so
Is ist possible to plot with strings as x-axe? I have the following file:
I'm trying to capture key-value pairs from strings that have the following form: a0=d235
Is it possible to pass *args to string.format? I have the following function: @classmethod
Is the following at all possible in c#? I have the following but the
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
I have the following PHP code which works out the possible combinations from a
I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){
I have the following query which returns a list of questions and the possible
Suppose I have the following code to collect all the possible combinations. abArray =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.