tags:

views:

56

answers:

4

How can i remove extra last semicolon in csv file.Because it's taking extra blank field.

Below is format of csv file.

12/12/2010;vvvvvv;JJJJ;BB;02070;kkkk yyyy tt oooo ;jjj iii;;mm nn nnnn;nnnnn gg bbbbb;xxxx ccc;;63610 iiiiiii;http://google.com;aaa bbb;;06 85 83 38 25;;[email protected];;2010-12-12;T;nnn;bbb-rr;rrr;rrr;3 C;N;

please help me it's urgent!!!!!!!!!

A: 

How abaout:

$str = trim($str, ";");

That will remove any semilocons from either the start or the end of the string. If you only want to remove from the end of the string, use rtrim instead of trim.

Pim Jager
i used this statment for taking csv file data $data = fgetcsv($handle, 1000, $fieldseparator)
Rupali
it gives me array
Rupali
A: 

You could also use

$str = substr($str,0-1);

This will remove whatever the last character is, semi-colon or otherwise.

kevtrout
This is not string.at run time it gives me array with last field blank value.i want remove that last blank field.
Rupali
Sorry, didn't realize you were dealing with an array. Be sure to mention it next time. I think Phill Pafford's correct with `array_pop`.
kevtrout
+1  A: 

After reading you comments I notice you said "remove the last element of an array".

array_pop($array); will remove the last element in the array

For a string use:

rtrim()

$text = '12/12/2010;vvvvvv;JJJJ;BB;02070;kkkk yyyy tt oooo ;jjj iii;;mm nn nnnn;nnnnn gg bbbbb;xxxx ccc;;63610 iiiiiii;http://google.com;aaa bbb;;06 85 83 38 25;;[email protected];;2010-12-12;T;nnn;bbb-rr;rrr;rrr;3 C;N;';

$trimmed = rtrim($text, ";");

echo $trimmed;
Phill Pafford
A: 

It seems you want to remove the last element from the array after taking the csv file data:

i used this statment for taking csv file data $data = fgetcsv($handle, 1000, $fieldseparator)

So, to remove the last element you could try:

unset array[count($array)-1];
xtracto