shane07 Posted February 8, 2008 Share Posted February 8, 2008 Hello to all I am having problem with an unknown character which is present at the end of the string. Actually I want to read data from the excel sheet. But some of the data in the excel sheets have probably the newline character which is causing problem. For an example: I have a string "he has a pen, a pencil and a sheet of paper". the string has a newline character at the end i.e. after the word 'paper'. Now when reading this string, it is automatically wrapped by double quotes and the string behaves as two different data as 'he has a pen, pencil and a sheet of paer' and '"'. I used trim($var,"\n"), instead of \n I used \x0A and I even tried for \t,\r but they are useless. Has somebody got the idea about this? Waiting for ur reply Thank You Quote Link to comment https://forums.phpfreaks.com/topic/90038-trim-function-not-working/ Share on other sites More sharing options...
aschk Posted February 8, 2008 Share Posted February 8, 2008 Yeah you want to use trim WITHOUT the 2nd parameter. Quoted from the PHP manual: Without the second parameter, trim() will strip these characters: * " " (ASCII 32 (0x20)), an ordinary space. * "\t" (ASCII 9 (0x09)), a tab. * "\n" (ASCII 10 (0x0A)), a new line (line feed). * "\r" (ASCII 13 (0x0D)), a carriage return. * "\0" (ASCII 0 (0x00)), the NUL-byte. * "\x0B" (ASCII 11 (0x0B)), a vertical tab. Quote Link to comment https://forums.phpfreaks.com/topic/90038-trim-function-not-working/#findComment-461634 Share on other sites More sharing options...
aschk Posted February 8, 2008 Share Posted February 8, 2008 Actually how are you reading this in? The reason I ask is that trim WON'T work if you're reading by lines (i.e. terminated by \n) because it will NEVER trim the line you're after because it doesn't contain the \n Quote Link to comment https://forums.phpfreaks.com/topic/90038-trim-function-not-working/#findComment-461637 Share on other sites More sharing options...
shane07 Posted February 8, 2008 Author Share Posted February 8, 2008 Actually how are you reading this in? The reason I ask is that trim WON'T work if you're reading by lines (i.e. terminated by \n) because it will NEVER trim the line you're after because it doesn't contain the \n I am using following codes to read from the excel sheet while(!feof($file)) { $line=fgets($file); But I have some of the data ending with newline that client accidentally put on Quote Link to comment https://forums.phpfreaks.com/topic/90038-trim-function-not-working/#findComment-461655 Share on other sites More sharing options...
aschk Posted February 8, 2008 Share Posted February 8, 2008 as far as I can remember from the documentation fgets splits like by line breaks (and/or carriage returns) so it's going to break up your "1" line (which actually is 2) into 2 lines as expected. What you probably want to use is the fgetcsv (assuming your spreadsheet is actually stored as a CSV file). This way you can split by separator, and enclose symbols. http://uk.php.net/fgetcsv e.g. $contents = fgetcsv($fileHandle, 4096, ",", "\""); This will create $contents as an array of lines. Hopefully this will sort out your problem. Quote Link to comment https://forums.phpfreaks.com/topic/90038-trim-function-not-working/#findComment-461748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.