views:

120

answers:

4

I am trying to find out the exact length of a string using strlen() in php 5.2. The string ($data) contains '\t' and '\n'.

echo strlen($data);

Code:

    // fetch table header
      $header = '';
      while ($fieldData = $result->fetch_field()) {
        $header .= $fieldData->name . "\t";
      }

      // fetch data each row, store on tabular row data
      while ($row = $result->fetch_assoc()) {
        $line = '';
        foreach($row as $value){
          if(!isset($value) || $value == ""){
            $value = "\t";
          }else{
            // important to escape any quotes to preserve them in the data.
            $value = str_replace('"', '""', $value);
            // needed to encapsulate data in quotes because some data might be multi line.
            // the good news is that numbers remain numbers in Excel even though quoted.
            $value = '"' . $value . '"' . "\t";
          }

          $line .= $value;
        }
        $data .= trim($line)."\n";
      }

      // this line is needed because returns embedded in the data have "\r"
      // and this looks like a "box character" in Excel
      $data = str_replace("\r", "", $data);

      // Nice to let someone know that the search came up empty.
      // Otherwise only the column name headers will be output to Excel.
      if ($data == "") {
        $data = "\nno matching records found\n";
      }

      // create table header showing to download a xls (excel) file
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=$export_filename");
      header("Cache-Control: public");
      header("Content-length: " . strlen($data); // tells file size
      header("Pragma: no-cache");
      header("Expires: 0");

  // output data
  echo $header."\n".$data;

This does not return the exact length (its less than the actual length). Please advice.

+1  A: 

Before getting length of string, delete '\t' and '\n' symbols from $data with str_replace or with other function like that if strlen() really has bug (I haven't checked that).

hey
I need \t and \n in the string.
kobra
`str_replace` doesn't change the original string.
Casey Hope
+2  A: 

Make sure you are not adding in the tab and newlines with single quotes. Single quotes around strings will evaluate \n to backslash and n, not newline character.

Also, make sure there is no special chars in your string, like Umlauts, because then it can happen that strlen('ü') is 2 indeed, because they are multibyte. Try strlen(utf8_decode('ü'));

Another alternative is to use mb_strlen() instead. Set mb_internal_encoding() correctly.

Gordon
Thanks. i have just pasted the code that creates the string. I am using double quotes.
kobra
+1  A: 

strlen returns the correct answer:

echo strlen("1\n2\t3");

// prints 5

You will need to examine your input more carefully.

webbiedave
I think when I save the data in Excel format, it translates \n and \t (non-printing characters. So if the tab size is 4 the above should result in length of 8. I think thats the problem.
kobra
@kobra: each tab is **always** 1 character. I don't think Excel ever messes with your tab characters.
BoltClock
+2  A: 

You are telling the user agent to expect strlen($data) and then actually sending $header."\n".$data! Try something like this at the end of your code...

  $output=$header."\n".$data;

  // create table header showing to download a xls (excel) file
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=$export_filename");
  header("Cache-Control: public");
  header("Content-length: " . strlen($output); // tells file size
  header("Pragma: no-cache");
  header("Expires: 0");

  // output data
  echo $output;
Paul Dixon