Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/180693-solved-field-subtract-first-word/
Share on other sites

That won't work, you forgot the delimiter :P '~^[^\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.

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

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.