code_junkie

Regular expression for parsing CSV in PHP

Nov 13th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. $csv_field = preg_replace('/"(.|$)/', '1', $csv_field);
  2.  
  3. preg_replace('/([W]){2}b/', '1', $csv)
  4.  
  5. $data = file_get_contents('test.csv');
  6. $pieces = explode("n", $data);
  7.  
  8. $html .= "<table border='1'>n";
  9. foreach (array_filter($pieces) as $line) {
  10.  
  11. $html .= "<tr>n";
  12. $keywords = preg_split('/,(?=(?:[^"]*"[^"]*")*(?![^"]*"))/', $line,-1,PREG_SPLIT_DELIM_CAPTURE);
  13.  
  14. foreach ($keywords as $col) {
  15. $html .= "<td>".trim($col, '"')."</td>n";
  16. }
  17. $html .= "</tr>n";
  18. }
  19. $html .= "</table>n";
  20.  
  21. preg_split('/,(?=(?:[^"]*"[^"]*")*(?![^"]*"))/', $line,-1,PREG_SPLIT_DELIM_CAPTURE);
  22.  
  23. preg_split('/'.$seperator.'(?=(?:[^"])*(?![^"]))/', $line,-1, PREG_SPLIT_DELIM_CAPTURE);
Add Comment
Please, Sign In to add comment