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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:16:59+00:00 2026-06-04T12:16:59+00:00

I am importing data from a CSV file using fgetcsv(), which yields an array

  • 0

I am importing data from a CSV file using fgetcsv(), which yields an array for each row. I then implode() this array and use mysql_real_escape_string() to create a string for use in a mysql query. It ends up looking like this:

\"2342352353\",\" \",\"\"

Some of the CSV files I am reading in have up to 200+ columns (rediculous, but true), so the insert statements become extremely long. To make them not so lengthy, I want to get rid of any spaces or blanks in the insert statement. So, the statement above should become:

\"2342352353\",,

So I am looking at 2 ways to solve this. First, I could walk through the array, and use trim() to find any entries with spaces and make them blank. Then I could do a str_replace() on the resulting string like so:

for ($x=0; $x<sizeof($data); $x++) {
    $data[$x] = trim($data[$x]) == ''  ?  ''  :  $data[$x];
}
$insert_string = '\\"' . mysql_real_escape_string(implode('","', $data)) . '\\"';
str_replace('\"\"', '', $insert_string);

Second approach is to skip walking through the array and just use regex to remove any occurrences of \" \", but there could be any number of spaces in the middle of the delimited quotes.

My question is 1st and foremost, how do I do this using regex? And secondly, which way would be faster?

Once I get the regex answer, I will do some benchmarking and will update with the results.

Thanks!

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

    What about this regex?

    \\"\s*\\"

    Used in your code above:

    preg_replace('/\\\"\s*\\\"/', '', $insert_string);

    The \s* matches any number of whitespace characters.

    And I’d eat my hat if regex was slower than iterating through your CSV file…

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

Sidebar

Related Questions

I am using PHP to expose vehicle GPS data from a CSV file. This
I am importing data from a CSV file. One of the fields has an
I'm importing data from a CSV that contains approx 350 columns. This CSV import
I'm importing data from a CSV file that comes from excel, but i can't
I have a web form used for importing data from a CSV file. It
I am having some difficulty with importing data from a .csv file. I am
I am importing approx 350,000 lines from a CSV file into MySQL using PHP
I'm trying to import some data (using oracle sql developer) from a .csv file
I am importing some data from an file (xls, csv, xml) wich will result
I'm importing some data from a CSV file, and numbers that are larger than

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.