Re: preg_split

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



1. A preg_ expression has to have the delimeters '/^([^,]+),\s?(.*)/'.

2. Why do you need this complex expression to split at a comma? This '/,/' would do the trick. And even simpler
		explode(',', $line);

Also your regex ^([^,]+) asks the perl regex parser to find the beginning of a line at which there is one or more instances of NO Comma. That is, [^ ] is a negation, so that [^0-9] means do not match any numbers.

Again, the rest of your regex asks for a match for anything else that follows 0 or 1 spaces, presumably the rest of the line. So that any possible split would gobble up everything after the first instance in which the beginning of a line did not start with a comma, which would mean that you would get null results.

3. Is there a reason why you are using the flag PREG_SPLIT_DELIM_CAPTURE?


Regular expressions are always tricky, so I hope the above comments are not totally out to lunch!

Google Kreme wrote:
OK, I have this file:

<web stuff>
<?php
      $CID_FILE= 'vonage.callers';
      $lines=file($CID_FILE);
      foreach ($lines as $line) {
$cid=preg_split('^([^,]+),\s?(.*)', $line, -1, PREG_SPLIT_DELIM_CAPTURE);

      }
 ?>
<web stuff>

the trouble is, $cid is empty. The actual file has lots of print lines, so I know that $lines is an array with each line for $CID_FILE as one element of the array and that $line gets the data from a single array element correctly.

What I want is for each line to be an array, split at the first comma. The regex works in BBEdit, where I can search for

^([^,]+),\s?(.*)

and replace with

\2 \1

and it does the right thing, over and over, so the regex is not the issue. It's something that I am not doing right/understanding in preg_split. I've also tried:

 $cid=preg_split('^([^,]+),', $line);
 $cid=preg_split('^([^,]+),\s?(.*)', $line, PREG_SPLIT_DELIM_CAPTURE);
 $cid=preg_split('^[^,]+,', $line, -1);
 $cid=preg_split('^[^,]+,', $line);

The actual code with the extra printing is here:

<http://akane.covisp.net/~kreme/vonage.phps>
<http://akane.covisp.net/~kreme/vonage.php>

and the sample data is

<http://akane.covisp.net/~kreme/vonage.callers>


--No one ever thinks of themselves as one of Them. We're always one of Us. It's Them that do the bad things.


--

_____________________
Myron Turner
http://www.mturner.org/XML_PullParser/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux