Re: Re: preg_split

From: Date: Fri, 03 Nov 2006 23:17:53 +0000
Subject: Re: Re: preg_split
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-244192@lists.php.net to get a copy of this message
I see, I miusunderstood, read too quickly, and thought you simply wanted to split at the comma. The suggestion that you use exlode(',',2) makes sense to me. You could do the same with
             preg_split ($pattern, $string, 2)
Or for a more complex regex, there's preg_match, which puts the results in an array of matches.  That would to me seem the more logical choice if you want to save parenthesized data. But I'm an old Perl programmer and there we don't have the extras that PHP gives to preg_split.  So, in Perl it's split() for splitting and m// for matching.  Perhaps in programming as in art, less is more.
Cheers, Myron Google Kreme 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.
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.
Yes, and ^([^,]+) means, From the start of the line, get 1 or more characters that are not a comma and put them in \01.
-- _____________________ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/

« previous php.general (#244192) next »