Issue with \r\n and \t - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Issue with \r\n and \t (/showthread.php?tid=9520) |
Issue with \r\n and \t - El Forum - 06-27-2008 [eluser]EEssam[/eluser] Hi guys, Can the following be written more nicely: ..... if ($this->input->post('newline') == '\r\n') { $newline = "\r\n"; } elseif ($this->input->post('newline') == '\t') { $newline = "\t"; } else { $newline = $this->input->post('newline'); } ..... echo $this->dbutil->csv_from_result($query, $delimiter, $newline); Thanks. Issue with \r\n and \t - El Forum - 06-27-2008 [eluser]parrots[/eluser] You could skip the else clause and just assign the value to the variable by default. Code: $newline = $this->input->post(’newline’); Issue with \r\n and \t - El Forum - 06-27-2008 [eluser]EEssam[/eluser] Hi parrots, The problem with the code it's not flexible at all. What if the user entered: \r\n\r\n or: \t\t How can this be solved. Thanks. Issue with \r\n and \t - El Forum - 06-27-2008 [eluser]dbashyal[/eluser] Code: $newline = preg_replace('/\r\n/', "\r\n", $newline); Issue with \r\n and \t - El Forum - 06-27-2008 [eluser]parrots[/eluser] \r\n\r\n would behave the same as it did in your original code -- it would be assigned to the newline variable. I think the problem you're seeing is that both of our if checks don't take account for cases like that. I'd suggest using regular expressions to do a replace on the string, it would be a lot more flexible... Code: $newline = $this->input->post(’newline’); The second line replaces all instances of '\t' with a tab character. You could do the same for \r, \n, etc. Removed the if block completely. Issue with \r\n and \t - El Forum - 06-27-2008 [eluser]EEssam[/eluser] Thank you very much, my code looks much better now. |