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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:14:51+00:00 2026-05-13T23:14:51+00:00

How can we validate a CSV file ? I have an CSV file of

  • 0

How can we validate a CSV file ?

I have an CSV file of structure:

Date;Id;Shown
15-Mar-10;231;345
15-Mar-10;232;346
and so on and on !!! approx around 80,000 rows. 

How can I validate this CSV file before starting the parsing using fgetcsv ?

  • 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-05-13T23:14:51+00:00Added an answer on May 13, 2026 at 11:14 pm

    I would not try to validate the file before hand : I would rather prefer going through it line by line, dealing with each line separately :

    • Reading one line
    • Verifying it’s OK
    • using the data
    • and going to next line.

    Now, what could “verify it’s OK” means ?

    • At least : make sure I can read the line as CSV, with my normal set of functions (maybe fgetcsv, maybe some other function specific to my project — anyway, if I cannot read one line with my function that reads hundreds, it’s probably because there’s a problem on that line)
    • Then, check for the number of fields
    • then, for each field, check if it contains "valid" data
      • mandatory ? optionnal ?
      • numeric ?
      • string ?
      • date ?
      • and so on
    • then, for each field, some more careful checks
      • for instance, for a "code" field : does it correspond to a value that’s legal for my application ?

    If all that goes OK — well, not much more to do, excepts use the data 😉

    And when you’re done with one line, just go repeat for the next one.

    Of course, if you want to either accept or reject a whole file before doing any database *(or anything like that)* write, you’ll have to :

    • parse the file, line by line, applying the "verifying" ideas
    • store the data of each line in memory
    • and, when the whole file has been read to memory,
      • either start using the data
      • or, if there’s been an error on one line, reject everything.

    In your specific case, you have three kind of fields :

    Date;Id;Shown
    15-Mar-10;231;345
    15-Mar-10;232;346
    

    From what I can guess :

    • The first one must be a date
      • Using some regex to validate that will not be easy : there are not the same number of days each month, there are many months, there is not the same number of days in february depending on the year, …
      • In such a case, I would probably try to parse the date with something like strtotime (not sure it’s ok for the format you’re using, though)
      • Or I would just explode the string
        • making sure there are three parts
        • that the third one is 2 digits
        • that the second one is one of Jan, Feb, Mar, …
        • That the first one corresponds to the correct number of days, depending on the two others
    • The second one :
      • must be an integer
      • must be a valid value, that exists in your database ?
        • If so, a simple SQL query will allow you to check that
    • For the third one, not really sure…
      • I’m guessing it has to be an integer ?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 399k
  • Answers 399k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Two problems here, first your IDs should not start with… May 15, 2026 at 4:04 am
  • Editorial Team
    Editorial Team added an answer Use the first option. That way, you can relocate your… May 15, 2026 at 4:04 am
  • Editorial Team
    Editorial Team added an answer The <form> tag adds some white space by default in… May 15, 2026 at 4:03 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.