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

  • Home
  • SEARCH
  • 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 9155487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:36:17+00:00 2026-06-17T12:36:17+00:00

I have a CSV file that I want to parse using PHP (don’t want

  • 0

I have a CSV file that I want to parse using PHP (don’t want to use php csv functions since they’re too slow for my case). I use preg_replace to select only the columns that I need and write output back to standard out. I have a pattern that look like that

preg_replace("/^\"([^\"]*)\",\"([^\"]*)\"(.*)$/m", "$1;$2", $content);

There are some lines in the CSV that are malformed. Is there a way to skip output for lines that don’t match the pattern above?

  • 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-17T12:36:19+00:00Added an answer on June 17, 2026 at 12:36 pm

    You could do it in two steps:

    First remove all lines that don’t match your regex:

    preg_replace("/^(?!\"([^\"]*)\",\"([^\"]*)\".*$).*\r?\n/m", "", $content);
    

    Then do the original regex replace.

    However, you need to be very careful. As soon as you get linebreaks and/or escaped quotes in your CSV fields, these regexes may break and destroy your file.

    Or imagine a malformed line like

    "foo","bar (missing quote!)
    "baz", "bam" (correct line)
    

    The regex would now delete both the incomplete and the correct line because [^\"]* also eats the newlines.

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

Sidebar

Related Questions

I have a transaction log file in CSV format that I want use to
I have a number of images and a CSV data file that I want
I have a CSV file which has unusual delimiters which I want to parse
I have a CSV file that I want to read with Ruby and create
I have a kind of csv file, with some extra parameters. I don't want
i have this file that i want to open, a csv file, and i
I have a csv file that required to be encrypted. I want to ask
Okay I have a csv file that gets parsed and displayed. I want to
I have csv file that has contents like this: 10,53073,0,0,'Y','2008-05-30 21:46:55',0,'2008-05-30 21:48:04',0,53071,2 I want
I have .csv file that contain 2 columns delimited with , . file.csv word1,word2

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.