|
Posted by Chung Leong on 06/23/06 14:33
frizzle wrote:
> Chung Leong wrote:
> > frizzle wrote:
> > > frizzle wrote:
> > > > Andy Hassall wrote:
> > > > > On 22 Jun 2006 13:21:50 -0700, "Chung Leong" <chernyshevsky@hotmail.com> wrote:
> > > > >
> > > > > >Andy Hassall wrote:
> > > > > >>
> > > > > >> There are undoubtably faster, more concise and cleverer ways of doing this.
> > > > > >
> > > > > >Which would involve, of course, regular expression :-)
> > > > > >
> > > > > >preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m,
> > > > > >PREG_SET_ORDER);
> > > > >
> > > > > Ah ha, was trying to work out how to get it with PCRE, knew it was possible
> > > > > :-)
> > > > >
> > > > > --
> > > > > Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
> > > > > http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
> > > >
> > > > I'm sorry, but i don't completely understand the logics in the
> > > > Disny-function, and i don't completely understand the logics in the
> > > > pattern of the preg_match_all part.
> > > >
> > > > Could someone please explain a little bit more ?
> > > >
> > > > Frizzle.
> > >
> > > Especially the "?P" - part in the pattern i cannot figure out ...
> > >
> > > Frizzle.
> >
> > The use of (?P<> ... ) indicates named capturing. It only affects how
> > the captured texts are referenced. Neither the P, the angle brackets,
> > or the text inside them are a part of the pattern. Thus
> >
> > /(?P<name>[^,]+),(?P<url>[^,]+)/
> >
> > is the exact same pattern as
> >
> > /([^,]+),([^,]+)/
> >
> > Compare the results from using the two and you'll see the difference.
> > Here a code snippet:
> >
> > <?php
> >
> > header('Content-Type: text/plain');
> > $list = "Google,http://www.google.com";
> > preg_match_all('/([^,]+),([^,]+)/', $list, $m1, PREG_SET_ORDER);
> > preg_match_all('/(?P<name>[^,]+),(?P<url>[^,]+)/', $list, $m2,
> > PREG_SET_ORDER);
> > var_dump($m1);
> > var_dump($m2);
> >
> > ?>
> >
> > Look in the manual for how preg_match_all() works.
>
> Ok, thanks, this named capturing is new to me, but looks kind of ideal!
> Thanks a lot guys!
>
> I have one little question left, not really essential, but to clean
> things up:
> can the preg-match-all be adjusted that output would be
>
> [0] => Array
> (
> [name] => Google
> [url] => http://www.google.com
> )
>
> instead of (current):
>
> [0] => Array
> (
> [0] => Google,http://www.google.com
> [name] => Google
> [1] => Google
> [url] => http://www.google.com
> [2] => http://www.google.com
> )
>
> per name/url -set ?
>
> Frizzle.
No, unfortunately. I wish an option like that exists.
[Back to original message]
|