Page 1 of 1

tokenizing with [^;]+, why it doesn't work?

Posted: Tue Jun 10, 2008 12:15 pm
by Ragnarok
I have a sort of csv (comma separated values) file, although in this case the values are separated by semicolons. I would like to get each one of those values and put it in a cell in a table. For that I wanted to match all characters that are different from a semicolon. I've tryed with everything, preg_match, preg_match_all, and I don't even remember what more. Nothing worked...

Code: Select all

<?php
  $f = fopen("test.txt", "r");
  echo '<html><body><table>';
  while(!feof($f)){
    echo '<tr>';
    $s = fgets($f);
    preg_match("/[^;]+)/", $s, $l);
 
    for ($i = 0; $i < sizeof($l); $i++)
      echo "<td>$l[i]</td>";
    echo '</tr>';
  }
  echo '</table></body></html>';
?>
What's wrong with it? Thanks a lot.

Re: tokenizing with [^;]+, why it doesn't work?

Posted: Tue Jun 10, 2008 12:35 pm
by GeertDD
I could give you a simple regex, probably using preg_split(), but why make things more difficult than they are. Don't know for sure but good old explode() may be your hero in this situation.

Code: Select all

$data = explode(';', 'comma;separated;values');