Re: Dividing, and keeping, text from the first space

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

 



On Wed, 2006-08-09 at 02:57 -0400, Robert Cummings wrote:
> On Wed, 2006-08-09 at 15:30 +0900, Dave M G wrote:
> > PHP List,
> > 
> > This regular expression stuff is way tricky.
> > 
> > Thanks to help from this list, I have an expression that will select the 
> > first word of a string, up to the first white space:
> > "#^(.*)\s#iU"
> > 
> > But after some consideration, I realized that I wanted to keep both 
> > parts of the original text. The first word, and then everything that 
> > came after it, should be divided and stored in separate variables.
> > 
> > I looked through the php.net manual, and found preg_split().
> > 
> > At first I thought using my original expression would work, if I 
> > included the PREG_SPLIT_DELIM_CAPTURE parameter. It would pull out the 
> > first word, but also keep it.
> > 
> > Turns out that's not true. It removes the first word and the space, and 
> > looks for something before and after and only finds something after. 
> > Thus, in my current situation, it returns only one variable, which 
> > contains everything after the first space.
> > 
> > I realized what I need to do in this case is not select everything up to 
> > the first space, but to find the first space without selecting what 
> > comes before it. So the expression I had wasn't suitable for preg_split().
> > 
> > So then, what is the right expression?
> > 
> > Looking back over previous discussion and resources about regular 
> > expression syntax, I thought I had to say:
> > Start at the beginning: ^
> > Ignore anything that isn't a space: [^\s]
> > Select the space character: \s
> > Be case insensitive and not greedy: iU
> > 
> > Thus, my expression should be (using hash marks, #, as delimiters):
> > #^[^\s]|s#iU
> > 
> > More specifically, my preg_split() syntax is:
> > $parts = preg_split("#^[^\s]|s#iU", $word, PREG_SPLIT_DELIM_CAPTURE);
> > 
> > But it returns a two element array, where the first element is empty, 
> > and the second element is the whole original string.
> > 
> > Where did I go wrong this time?
> 
> Use preg_match() and pay special attention to the manual as it refers to
> the third parameter :) The expression you need follows:
> 
>    "#^([^\\s]*)\\s(.*)$#U"
> 
> You don't need the insensitive modifier btw since you aren't actually
> matching anything that is applicable.

Also you don't need the ungreedy modifier either since you can't
overshoot with the above expression.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
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