Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/90038-trim-function-not-working/
Share on other sites

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.

 

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.