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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Ragnarok
Forum Newbie
Posts: 1
Joined: Tue Jun 10, 2008 12:07 pm

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

Post 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.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

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

Post 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');
Post Reply