Dividing, and keeping, text from the first space

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

 



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?

Thank you for all your time and help.

--
Dave M G

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