开发者

How can I remove extra last semicolon in csv file?

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;;aaa@vv.开发者_开发问答com;;2010-12-12;T;nnn;bbb-rr;rrr;rrr;3 C;N;


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;;aaa@vv.com;;2010-12-12;T;nnn;bbb-rr;rrr;rrr;3 C;N;';

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

echo $trimmed;


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.


You could also use

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

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


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜