#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7

    Don't undertand preg & str replace in this example


    Well, most the rest of the code I have edited to fit my needs. I get how everything is working, except for the replace items near end. If I am correct, would this not mess up a column in which starts with a dollar sign to repressent currency? And with the preg_replace, wouldn't the third search item be replacing the \n that we are entering ourselves?

    php Code:
    // Header info for browser
    header("Content-Type: application/xls");
    header("Content-Disposition: attachment; filename=timeoff.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    // Define separator (tab)
    $sep = "\t";
    // Define column names
    print("Start Date\tEnd Date\tDays Used\tReason\n");
    // Create the rows
    while($statement->fetch()) {
      $schema_insert = "";
      for($i=1; $i<5;$i++) {
        if(!isset(${'col' . $i})) {
          $schema_insert .= "NULL" . $sep;
        } elseif(${'col' . $i} != "") {
          $schema_insert .= ${'col' . $i} . $sep;
        } else {
          $schema_insert .= $sep;
        }
      }
      $schema_insert = str_replace($sep."$", "", $schema_insert);
      $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
      $schema_insert .= "\t";
      print(trim($schema_insert));
      print "\n";
    }
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    sorry, but I think most of us didn't follow every step of your project. So if you want help, you'll need to explain what you're trying to do.

    It's clear that you turn the result set of some query into a CSV. But why you do wanna remove the dollar signs in the first place? Why do you add an extra tab after every row (and remove it immediately)?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    That is kinda my question. This was originally a snippet from an example elsewhere that I just edited as needed. I am seeing it as you seemed to have asked, but since I was thinking "Why remove something you want or have been adding the whole time?" I figured maybe my understanding of what the two lines were doing was wrong. The only thing I guessed with the dollar sign was maybe the person who initially wrote it did it to maybe avoid a variable afterward, but that kinda made no sense.

    This just fetches rows from a MySQL table and is to list them in an Excel file. So, just making sure I am seeing it right, and I can safely remove |\n| as well as the whole str_replace line.
    Last edited by Triple_Nothing; September 2nd, 2013 at 03:13 PM.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Well, in this case I have to tell you the same thing we tell everybody:

    Do not copypaste code you found somewhere on the Internet.

    I mean, don't you find it kinda silly that we're sitting here, trying to figure out what some random PHP programmer somewhere on this planet may have thought when writing this piece of code? Write your own code. It's perfectly fine to get inspiration from other people, but running stuff when you don't know what it does and why isn't.

    This is a rather ugly hack, anyway. Obviously, the output is not an Excel file. It's a plain CSV masquerading as an Excel file. I guess that most modern applications can handle this misdeclaration and trigger some kind of import mechanism, but this isn't exactly a correct solution.

    So I suggest that you throw this stuff away and write your own code. The code is neither large nor complex, so it shouldn't take too much time. Either grab an Excel library and generate a real Excel file. Or fix the declation. It should be a .csv file with type text/csv. It might also be a good idea to use real column names instead of those stupid col<n>. Simply make an array to specify the order.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Np. I just learn from examples, even on PHP.net. If they didn't have their examples, wow would I go crazy. :-P

IMN logo majestic logo threadwatch logo seochat tools logo