Autodetect Presence of CSV Headers in a File

后端 未结 6 1544
心在旅途
心在旅途 2020-12-25 13:23

Short question: How do I automatically detect whether a CSV file has headers in the first row?

Details: I\'ve written a small CSV parsing engine th

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-25 13:29

    If you CSV has a header like this.

    ID, Name, Email, Date 1, john, john@john.com, 12 jan 2020

    Then doing a filter_var(str, FILTER_VALIDATE_EMAIL) on the header row will fail. Since the email address is only in the row data. So check header row for an email address (assuming your CSV has email addresses in it).

    Second idea. http://php.net/manual/en/function.is-numeric.php Check header row for is_numeric, most likely a header row does not have numeric data in it. But most likely a data row would have numeric data.

    If you know you have dates in your columns, then checking the header row for a date would also work.

    Obviously you need to what type of data you are expecting. I am "expecting" email addresses.

提交回复
热议问题