EternalSorrow Posted November 7, 2009 Share Posted November 7, 2009 I've been searching for several hours for an answer to this simple problem, but no luck, so I've come groveling here to seek guidance. All I want to know is when a $field is retrieved from the database, how can I subtract, skip, trim or essentially eliminate the first word from a string? Here's an example of what I want with the string of the $field and the output I'm going searching for: $string = "The Scarlet Pimpernel"; $output = "Scarlet Pimpernel"; Be aware the first word could be anything, not necessarily the word 'the.' Quote Link to comment Share on other sites More sharing options...
Alex Posted November 7, 2009 Share Posted November 7, 2009 <?php $string = "The Scarlet Pimpernel"; $words = str_word_count($string, 1); array_shift($words); echo implode(' ', $words); Quote Link to comment Share on other sites More sharing options...
.josh Posted November 7, 2009 Share Posted November 7, 2009 $string = preg_replace('^[^\s]*\s','',$string); Quote Link to comment Share on other sites More sharing options...
Alex Posted November 7, 2009 Share Posted November 7, 2009 That won't work, you forgot the delimiter '~^[^\s]*\s~' I was curious so I tested: $words = str_word_count($string, 1); array_shift($words); implode(' ', $words); vs $string = preg_replace('~^[^\s]*\s~','',$string); regex takes about ~1.6 times longer. Not that it matters at this scale. Quote Link to comment Share on other sites More sharing options...
.josh Posted November 7, 2009 Share Posted November 7, 2009 argh that's like the 3rd post recently where i forgot delims Quote Link to comment Share on other sites More sharing options...
.josh Posted November 7, 2009 Share Posted November 7, 2009 I'm kinda surprised the preg_match wasn't faster, since str_word_count has to evaluate the whole string. I wonder if $string = explode(' ',$string); array_shift($string) $string = implode(' ',$string); might be faster than str_word_count Quote Link to comment Share on other sites More sharing options...
Alex Posted November 7, 2009 Share Posted November 7, 2009 I tested it, str_word_count seems to be ~1.05 times slower. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 7, 2009 Share Posted November 7, 2009 ltrim(strstr(' ', $foo)); substr(strstr(' ', $foo), 1); substr($foo, strpos($foo, ' ')+1); for ($i = 0; !isset($foo[$i]) || $foo[$i] != ' '; ++$i) {} echo substr($foo, $i+1); for ($i = 0, $l = strlen($foo); $i < $l && $foo[$i] != ' '; ++$i) {} substr($foo, $i+1); Quote Link to comment Share on other sites More sharing options...
Alex Posted November 7, 2009 Share Posted November 7, 2009 ltrim(strstr(' ', $foo)); Do you mean ltrim($string, strstr(' ', $foo, true)) ? That'll only work in PHP 5.3+ though. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 7, 2009 Share Posted November 7, 2009 Do you mean ltrim($string, strstr(' ', $foo, true)) ? Nope. strstr things you the stuff after the needle including the needle. Quote Link to comment Share on other sites More sharing options...
.josh Posted November 7, 2009 Share Posted November 7, 2009 except you have arguments backwards in strstr Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 7, 2009 Share Posted November 7, 2009 Oh yeah well, it got the point across anyways Quote Link to comment Share on other sites More sharing options...
Alex Posted November 7, 2009 Share Posted November 7, 2009 oic. I thought you were trying to get 'The' (the first word in the string) and use that as the charlist in ltrim(). Just wasn't thinking. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 8, 2009 Share Posted November 8, 2009 One more: strtok($foo, ' '); echo strtok(''); Quote Link to comment Share on other sites More sharing options...
salathe Posted November 8, 2009 Share Posted November 8, 2009 For the regex approach, you could just use /^\S+\s+/ (using \S instead of [^\s]). Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 8, 2009 Share Posted November 8, 2009 <?php $string = "The Scarlet Pimpernel"; $words = str_word_count($string, 1); array_shift($words); echo implode(' ', $words); Bad idea, since it removes punctuation. Quote Link to comment Share on other sites More sharing options...
EternalSorrow Posted November 8, 2009 Author Share Posted November 8, 2009 We'll see how long the first post suggestion works on the site. Thanks for all the help and the great learning experience everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.