views:

40

answers:

3

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.

A: 

Sorry but can you give more information, not sure what you mean.

PHPology
This should have been a comment, not an answer...
xil3
+2  A: 

You can try this:

$csvArray = explode(",", $csv);
array_shift($csvArray);
$newCsv = implode(",", $csvArray);
xil3
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](http://stackoverflow.com/questions/3117604/why-is-calling-a-function-such-as-strlen-count-etc-on-a-referenced-value-so-sl), it's previously been a [security risk](http://php-security.org/category/vulnerabilities/index.html) in the PHP core (check out the "interruption" bugs...)
Charles
Won't work if any of the values actually contain a comma, whereas the built in CSV handling functions can handle this
Mark Baker
array_shift does request a reference.
xil3
@Mark Baker: yea true, but that was just a quick solution based on examples he posted.
xil3
This did the trick, and thanks for the warning.
Yaaqov
`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.
Charles
Yeah, I did notice the error message, which is why I removed it. Thanks for the heads up, though.
xil3
+1  A: 

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

Xeross