Skip to content
Advertisement

How to keep only a part of an explode of a csv column in php?

I have this php code that allows me to read a csv file and add 10 columns at the end to decompose the column $data[23].

This column contains for example :

JavaScript

I would like these lines there:

JavaScript

I would like to display and keep only the part to the left of the dash. For the moment my code works but just separates this information on all the columns, how can I have only the part to the left of the dash?

JavaScript

Advertisement

Answer

To process a single row, you need to first split the string by comma. Then you need to take each result of that split separately, and split it by dash. Finally, add the first item from each of those dash splits to your output – they will be the strings you are looking for.

e.g. here’s a function which would split a single row as per your sample data:

JavaScript

Example of usage:

JavaScript

Live demo: http://sandbox.onlinephpfunctions.com/code/b1b7b60da02b9ef13c5747414016aa0fcf296249

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement