Re: Re: preg_split

From: Date: Sat, 04 Nov 2006 00:13:46 +0000
Subject: Re: Re: preg_split
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-244194@lists.php.net to get a copy of this message
On 03 Nov 2006, at 15:56 , Stut wrote:
Google Kreme wrote:
On 03 Nov 2006, at 12:32 , Myron Turner wrote:
2. Why do you need this complex expression to split at a comma? This '/,/' would do the trick. And even simpler
        explode(',', $line);
Because I need to split only at the FIRST comma.
explode(',', $line, 2); http://php.net/explode
Heh. This is why I post here. Last time I posted I was told to use preg_split. Explode is simpler. BTW, preg_split works it I change $line to trim($line), which is needed for explode too, else the 'name' part ends up with a return embedded. Code as it stands now: <?php
      $CID_FILE= '/home/user/caller_id.txt';
      echo ' <h1>PHP Caller ID Info for procmail ('.$CID_FILE.') </h1>';
      $lines=file($CID_FILE);
      $i=0;
      foreach ($lines as $line) {
         $cid[$i] = explode(',',trim($line),2);
         // This line works too
         // $cid[$i]=preg_split('/^([^,]+),\s?(.*)/', trim($line), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
         $i=$i+1;
      }
      echo '<form action="'.  $_SERVER['PHP_SELF'] .'" method="post">';
      echo '<table>';
      $i=1;
      foreach ($cid as $line) {
         $count = substr('00'.$i,-3);
         echo '
         <tr>
           <td>
             <label for="phone'.$count.'">Phone #'.$count.':</label>
             <input name="phone'.$count.'" maxlength="14" value="'. $line['0'].'" />
           </td>
           <td>
             <label for="name'.$count.'">Name #'.$count.':</label>
             <input name="name'.$count.'" maxlength="34" value="'. $line['1'].'" />
         </tr>';
         $i=$i+1;
      }
      echo '</table>
      <input name="submit" type="submit" tabindex="99" a />
      </form>
       ';
?> Course, there's nothing there to process the submit as yet. And yes, I will combine these into one for-each and a function call. -- A marriage is always made up of two people who are prepared to swear that only the other one snores.

« previous php.general (#244194) next »