On Wed, 2006-08-09 at 08:11 -0700, Kevin Murphy wrote:
> 
> On Aug 8, 2006, at 11:30 PM, 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.
> 
> Now, I would do this different. Probably "wrong" but instead of a  
> regular expression here, I would do something like this:
> 
> $array = explode(" ",$string);
> $first_word = $array[0];
> $rest_words = substr_replace($string,"",0,strlen($first_word));

This presumes a space and not just whitespace. What does the adaptation
look like if you account for space, tab, newline, carriage return? >:)

That said, if we were to only match on a space character:

<?php

$text = "The nimble fox jumped over the river.";

if( ($splitPoint = strpos( $text, ' ' )) !== false )
{
    $word = substr( $text, 0, $splitPoint );
    $rest = substr( $text, $splitPoint + 1 );
}
else
{
    $word = $text;
    $rest = '';
}

echo "[$word] [$rest]\n";

?>

But then yours is more concise (though not as concise as the regular
expression). This is just faster. Depends on what you want ;)

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

Reply via email to