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 7845589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:16:53+00:00 2026-06-02T17:16:53+00:00

I have a postcode array: $firstPostcode = array( ‘se1’, ‘ec2a’, ‘se16’, ); I need

  • 0

I have a postcode array:

$firstPostcode = array(
    'se1',
    'ec2a',
    'se16',
);

I need to validate users first 4 chars of a uk postcode against the above array.

Problem I have is I need to loop through one char at a time. Some input maybe 4 chars or 3 with a space.

I have tried preg match and in array but can’t solve it after hours of googling

Thanks for any advice.

The problem I am having is its matching the whole array not starting 4 chars

  • 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-02T17:16:55+00:00Added an answer on June 2, 2026 at 5:16 pm

    You should be able to match with a single regexp:

    $firstPostcode = array(
        'se1',
        'ec2a',
        'se16',
    );
    $rx = '!^\s*('.implode('|', $firstPostcode).')!i';
    
    if (preg_match($rx, $input)){
        # postcode matches!
    }
    

    The creates a regexp like this:

    !^\s*(se1 |ec2a|se16)!i
    

    This matches one of your prefixes at the start of the string (skipping any spaces first) and is case-insensitive.

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

Sidebar

Related Questions

I have the following code: $postcode = $form->createElement('text', 'postcode'); $postcode->setLabel('Post code:'); $postcode->addValidator('regex', false, array('/^[a-z]{1,3}[0-9]{1,3}
I have a table of contacts and a table of postcode data. I need
I have a table with postcode districts such as SA1,SA3,SA11, LL41,NP20 and I need
I have a contacts table which contains fields such as postcode , first name
I have Postcode in my large database, which contains values like SL5 9JH, LU1
I have an address finder system whereby a user enters a postcode, if postcode
I have created a PHP form which requires the user to select a postcode
i have this MySQL statement from a search page, the user enters there postcode
I have a table of names and addresses, which includes a postcode column. I
I need to re-format a list of UK postcodes and have started with the

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.