0

I'm simply looking to take the first csv out of a string, using PHP. Here are a few examples:

  • "Sea Bass, Floured Or Breaded, Fried" => "Floured Or Breaded, Fried"
  • "Tuna, Fresh, Baked Or Broiled" => "Fresh, Baked Or Broiled"
  • "Croaker, Breaded Or Battered, Baked" => "Breaded Or Battered, Baked"

And so on...

Thank you.

4
  • Sorry but can you give more information, not sure what you mean. Commented Jul 5, 2010 at 16:12
  • Do you already have code that loads the CSV file into memory? If so, would you please post the relevant portion? Commented Jul 5, 2010 at 16:13
  • Show the CSV data, because what this is, isn't separating the two elements, which means you want to modify data inside a csv element rather than remove an element. Commented Jul 5, 2010 at 16:15
  • As Ben says, how are you populating your string.... if it is from a file, then PHP's fgetcsv() function can simplify things for you Commented Jul 5, 2010 at 16:21

2 Answers 2

2

You can try this:

$csvArray = explode(",", $csv);
array_shift($csvArray);
$newCsv = implode(",", $csvArray);
Sign up to request clarification or add additional context in comments.

6 Comments

You don't need to (and should not ever) pass an array (or anything, really) by reference to a built-in function unless the function expressly requests a reference. Besides being counter-intuitively slow, it's previously been a security risk in the PHP core (check out the "interruption" bugs...)
Won't work if any of the values actually contain a comma, whereas the built in CSV handling functions can handle this
array_shift does request a reference.
@Mark Baker: yea true, but that was just a quick solution based on examples he posted.
array_shift works with a reference, but you do not need to expressly create a reference to make it work. What you'd done is called "call-time pass-by-reference", and is considered such a bad practice that they added an error message just to tell people to stop doing it.
|
1

I suggest using str_getcsv or something similar that parses csv, this will take into account any quotation marks and commas in the CSV string. after that you can just array_shift

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.